-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
246 lines (222 loc) · 6.5 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"context"
"database/sql"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"time"
"cloud.google.com/go/bigquery"
"github.com/lib/pq"
"google.golang.org/api/option"
)
const Version = "1.4.0"
const CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"
var (
pgConn = flag.String("uri", "postgres://postgres@127.0.0.1:5432/postgres?sslmode=disable", "postgres connection uri")
pgSchema = flag.String("schema", "public", "postgres schema")
pgTable = flag.String("table", "", "postgres table name")
datasetId = flag.String("dataset", "", "BigQuery dataset")
projectId = flag.String("project", "", "BigQuery project id")
partitions = flag.Int("partitions", -1, "Number of per-day partitions, -1 to disable")
versionFlag = flag.Bool("version", false, "Print program version")
labelKey = flag.String("label-key", "", "Combined with --label-value, name for label on BigQuery table metadata")
labelValue = flag.String("label-value", "", "Combined with --label-key, value for label on BigQuery table metadata")
exclude = flag.String("exclude", "", "columns to exclude")
ignoreTypes = flag.Bool("ignore-unknown-types", false, "Ignore unknown column types")
updateOpts = flag.String("schema-update-opts", "", "loader schema update options, comma separated")
)
type Column struct {
Name string
Type string
IsNullable string
}
func (c *Column) ToFieldSchema() (*bigquery.FieldSchema, error) {
var f bigquery.FieldSchema
f.Name = c.Name
f.Required = c.IsNullable == "NO"
switch c.Type {
case "varchar", "bpchar", "text", "citext", "xml", "cidr", "inet", "uuid", "bit", "varbit", "bytea", "money", "jsonb":
f.Type = bigquery.StringFieldType
case "int2", "int4", "int8":
f.Type = bigquery.IntegerFieldType
case "float4", "float8", "numeric":
f.Type = bigquery.FloatFieldType
case "bool":
f.Type = bigquery.BooleanFieldType
case "timestamptz":
f.Type = bigquery.TimestampFieldType
case "date":
f.Type = bigquery.DateFieldType
case "timestamp":
f.Type = bigquery.DateTimeFieldType
case "time":
f.Type = bigquery.TimeFieldType
default:
return nil, errors.New("Unknown column type: " + c.Type)
}
return &f, nil
}
func schemaFromPostgres(db *sql.DB, schema, table string) bigquery.Schema {
rows, err := db.Query(`SELECT column_name, udt_name, is_nullable FROM information_schema.columns WHERE table_schema=$1 AND table_name=$2 ORDER BY ordinal_position`, schema, table)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
excludes := strings.Split(*exclude, ",")
var c Column
var s bigquery.Schema
for rows.Next() {
if err := rows.Scan(&c.Name, &c.Type, &c.IsNullable); err != nil {
log.Fatal(err)
}
if !contains(c.Name, excludes) {
f, err := c.ToFieldSchema()
if err == nil {
s = append(s, f)
} else if !*ignoreTypes {
panic(err)
}
}
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return s
}
func contains(s string, haystack []string) bool {
for i := 0; i < len(haystack); i++ {
if s == haystack[i] {
return true
}
}
return false
}
func columnsFromSchema(schema bigquery.Schema) string {
cols := make([]string, len(schema))
for i, field := range schema {
cols[i] = pq.QuoteIdentifier(field.Name)
if field.Type == bigquery.StringFieldType {
cols[i] = cols[i] + "::text"
}
}
return strings.Join(cols, ",")
}
func getRowsStream(db *sql.DB, schema bigquery.Schema, pgSchema, table string) io.Reader {
rows, err := db.Query(fmt.Sprintf(`SELECT row_to_json(t) FROM (SELECT %s FROM %s.%s) AS t`, columnsFromSchema(schema), pq.QuoteIdentifier(pgSchema), pq.QuoteIdentifier(table)))
if err != nil {
log.Fatal(err)
}
reader, writer := io.Pipe()
go func() {
defer rows.Close()
defer writer.Close()
for rows.Next() {
var b []byte
rows.Scan(&b)
writer.Write(b)
writer.Write([]byte{'\n'})
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}()
return reader
}
func init() {
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
if *versionFlag {
fmt.Fprintf(os.Stderr, "%s version: %s (%s on %s/%s; %s)\n", os.Args[0], Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
os.Exit(0)
}
if (*labelKey != "" && *labelValue == "") || (*labelKey == "" && *labelValue != "") {
log.Fatal("!! a label key and a label value are both required if either is passed")
}
keyfile := os.Getenv(CREDENTIALS)
if keyfile == "" {
log.Fatal("!! missing ", CREDENTIALS)
}
opt := option.WithServiceAccountFile(keyfile)
ctx := context.Background()
client, err := bigquery.NewClient(ctx, *projectId, opt)
if err != nil {
log.Fatal(err)
}
db, err := sql.Open("postgres", *pgConn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
partitioned := *partitions > -1
schema := schemaFromPostgres(db, *pgSchema, *pgTable)
table := client.Dataset(*datasetId).Table(*pgTable)
if _, err := table.Metadata(ctx); err != nil {
metadata := &bigquery.TableMetadata{Schema: schema}
if partitioned {
metadata.TimePartitioning = &bigquery.TimePartitioning{
Expiration: time.Duration(*partitions) * 24 * time.Hour,
}
}
if *labelKey != "" && *labelValue != "" {
labels := make(map[string]string)
labels[*labelKey] = *labelValue
metadata.Labels = labels
}
if err := table.Create(ctx, metadata); err != nil {
log.Fatal(err)
}
} else if *labelKey != "" && *labelValue != "" {
var tm bigquery.TableMetadataToUpdate
tm.SetLabel(*labelKey, *labelValue)
_, err := table.Update(ctx, tm, "")
if err != nil {
log.Fatal(err)
}
}
if partitioned {
table.TableID += time.Now().UTC().Format("$20060102")
}
log.Println("TableID", table.TableID)
f := getRowsStream(db, schema, *pgSchema, *pgTable)
rs := bigquery.NewReaderSource(f)
rs.SourceFormat = bigquery.JSON
rs.MaxBadRecords = 0
rs.Schema = schema
loader := table.LoaderFrom(rs)
loader.CreateDisposition = bigquery.CreateNever
loader.WriteDisposition = bigquery.WriteTruncate
if *updateOpts != "" {
loader.SchemaUpdateOptions = strings.Split(*updateOpts, ",")
}
job, err := loader.Run(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("JobID", job.ID())
for {
status, err := job.Status(ctx)
if err != nil {
log.Fatal(err)
}
if status.Statistics.Details != nil {
details := status.Statistics.Details.(*bigquery.LoadStatistics)
log.Println("OutputBytes", details.OutputBytes)
log.Println("OutputRows", details.OutputRows)
}
if status.Done() {
if status.Err() != nil {
log.Fatal(status.Err())
}
break
}
time.Sleep(100 * time.Millisecond)
}
}