-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (126 loc) · 3.47 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
)
var operation string
var startDate, endDate string
var projectID, dataset, table string
var gcsPrefix string
var startTime, endTime time.Time
func init() {
flag.StringVar(&operation, "o", "", "operation to perform, either import or export")
flag.StringVar(&startDate, "s", "", "start date to import/export, YYYY-MM-DD format")
flag.StringVar(&endDate, "e", "", "end date to import/export, YYYY-MM-DD format")
flag.StringVar(&projectID, "p", "", "target project")
flag.StringVar(&dataset, "d", "", "target dataset")
flag.StringVar(&table, "t", "", "target table name")
flag.StringVar(&gcsPrefix, "g", "", "prefix of GCS source/destination")
}
func main() {
flag.Parse()
if err := checkFlags(operation, startDate, endDate, dataset, table, gcsPrefix); err != nil {
fmt.Printf(err.Error() + "\n")
flag.Usage()
os.Exit(1)
}
ctx := context.Background()
bqc, err := bigquery.NewClient(ctx, projectID)
if err != nil {
fmt.Printf("could not initialize BigQuery client, %s\n", err.Error())
os.Exit(1)
}
if err := run(ctx, bqc, startTime, endTime, dataset, table, gcsPrefix); err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
os.Exit(1)
}
}
func run(ctx context.Context, bqc *bigquery.Client, startTime, endTime time.Time, dataset, table, gcsPrefix string) error {
if err := checkDataset(ctx, bqc, dataset); err != nil {
return err
}
if err := checkTable(ctx, bqc, dataset, table); err != nil {
return err
}
if operation == "import" {
return runImport(ctx, bqc, startTime, endTime, dataset, table, gcsPrefix)
}
return runExport(ctx, bqc, startTime, endTime, dataset, table, gcsPrefix)
}
func checkDataset(ctx context.Context, bqc *bigquery.Client, dataset string) error {
it := bqc.Datasets(ctx)
found := false
for {
cur, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
if cur.DatasetID == dataset {
found = true
break
}
}
if !found {
return fmt.Errorf("dataset %s not found\n", dataset)
}
return nil
}
func checkTable(ctx context.Context, bqc *bigquery.Client, dataset, table string) error {
it := bqc.Dataset(dataset).Tables(ctx)
found := false
for {
cur, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
if cur.TableID == table {
found = true
break
}
}
if !found {
return fmt.Errorf("table %s not found\n", table)
}
return nil
}
func checkFlags(operation, startDate, endDate, dataset, table, gcsPrefix string) error {
if operation != "import" && operation != "export" {
return fmt.Errorf("invalid operation")
}
dateLayout := "2006-01-02"
var err error
if startTime, err = time.Parse(dateLayout, startDate); err != nil {
return fmt.Errorf("invalid start date")
}
if endTime, err = time.Parse(dateLayout, endDate); err != nil {
return fmt.Errorf("invalid end date")
}
if startTime.After(endTime) {
return fmt.Errorf("the start date you entered is after the end date")
}
if projectID == "" {
return fmt.Errorf("invalid project")
}
if dataset == "" || table == "" {
return fmt.Errorf("invalid BigQuery dataset of table name")
}
gcsScheme := "gs://"
if len(gcsPrefix) < len(gcsScheme) || gcsPrefix[:len(gcsScheme)] != gcsScheme {
return fmt.Errorf("gcs prefix must start with gs://")
}
// trim if '/' is at the last of gcsPrefix
gcsPrefix = strings.TrimRight(gcsPrefix, "/")
return nil
}