forked from Shopify/bigtable-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigtable-emulator.go
103 lines (87 loc) · 3.46 KB
/
bigtable-emulator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"cloud.google.com/go/bigtable/bttest"
"cloud.google.com/go/bigtable"
"google.golang.org/grpc"
"strings"
"google.golang.org/api/option"
"flag"
"errors"
)
func main() {
cfs := flag.String("cf", "", "Optional: the column families to create at startup. Format: a series of <instance>.<table>.<column familiy>, comma separated. Ex: \n docker run -d spotify/bigtable-emulator -cf dev.records.data,dev.records.metadata")
flag.Parse()
srv, err := bttest.NewServer("0.0.0.0:9035")
if err != nil {
fmt.Fprintf(os.Stderr, "error starting the server: %v\n", err)
return
}
defer srv.Close()
if err = createColumnFamiliies(*cfs); err != nil {
fmt.Fprintf(os.Stderr, "error creating the column familiies: %v\n", err)
return
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
fmt.Printf("Bigtable emulator running on %s\n", srv.Addr)
<-sigs
fmt.Println("done")
}
func createColumnFamiliies(specifications string) error {
if specifications == "" {
return nil
}
ctx := context.Background()
conn, err := grpc.Dial("localhost:9035", grpc.WithInsecure())
if err != nil {
return fmt.Errorf("could not connect to bigtable emulator: %v", err)
}
for _, specification := range strings.Split(specifications, ",") {
specificationElements := strings.Split(specification, ".")
if len(specificationElements) != 3 {
return errors.New("format of column family to create is <instance>.<table>.<column family>")
}
instance := specificationElements[0]
table := specificationElements[1]
columnFamily := specificationElements[2]
client, err := bigtable.NewAdminClient(ctx, "dev", instance, option.WithGRPCConn(conn))
if err != nil {
return fmt.Errorf("failed to create admin client: %v", err)
}
tables, err := client.Tables(ctx)
if !tableExists(tables, table) {
if err = client.CreateTable(ctx, table); err != nil {
return err
}
}
tableInfo, err := client.TableInfo(ctx, table)
if !columnFamilyExists(tableInfo.FamilyInfos, columnFamily) {
fmt.Printf("creating %v.%v.%v column family\n", instance, table, columnFamily)
if err := client.CreateColumnFamily(ctx, table, columnFamily); err != nil {
return err
}
}
}
return nil
}
func tableExists(tables []string, table string) bool {
for _, item := range tables {
if item == table {
return true
}
}
return false
}
func columnFamilyExists(columnFamilies []bigtable.FamilyInfo, columnFamily string) bool {
for _, family := range columnFamilies {
if family.Name == columnFamily {
return true
}
}
return false
}