/
output_bigquery.go
321 lines (269 loc) · 8.44 KB
/
output_bigquery.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package gallon
import (
"cloud.google.com/go/bigquery"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-logr/logr"
"github.com/google/uuid"
"google.golang.org/api/option"
"gopkg.in/yaml.v3"
"os"
"reflect"
"strings"
"time"
)
type OutputPluginBigQuery struct {
logger logr.Logger
client *bigquery.Client
endpoint *string
datasetId string
tableId string
schema bigquery.Schema
deserialize func(interface{}) ([]bigquery.Value, error)
deleteTemporaryTable bool
}
func NewOutputPluginBigQuery(
client *bigquery.Client,
endpoint *string,
datasetId string,
tableId string,
schema bigquery.Schema,
deserialize func(interface{}) ([]bigquery.Value, error),
deleteTemporaryTable bool,
) *OutputPluginBigQuery {
return &OutputPluginBigQuery{
client: client,
endpoint: endpoint,
datasetId: datasetId,
tableId: tableId,
schema: schema,
deserialize: deserialize,
deleteTemporaryTable: deleteTemporaryTable,
}
}
type bqRecordWrapper map[string]bigquery.Value
var _ bigquery.ValueSaver = bqRecordWrapper{}
func (w bqRecordWrapper) Save() (row map[string]bigquery.Value, insertID string, err error) {
return w, "", nil
}
var _ OutputPlugin = &OutputPluginBigQuery{}
func (p *OutputPluginBigQuery) ReplaceLogger(logger logr.Logger) {
p.logger = logger
}
func (p *OutputPluginBigQuery) waitUntilTableCreation(ctx context.Context, tableId string) error {
timeout := time.After(300 * time.Second)
ticker := time.NewTicker(10 * time.Second)
for {
select {
case <-timeout:
return fmt.Errorf("timeout while waiting for table %v to be created", p.tableId)
case <-ticker.C:
if meta, err := p.client.Dataset(p.datasetId).Table(tableId).Metadata(ctx); err != nil {
p.logger.Info(fmt.Sprintf("waiting for table %v to be created, %v", p.tableId, meta))
continue
}
return nil
}
}
}
func (p *OutputPluginBigQuery) Load(
ctx context.Context,
messages chan interface{},
errs chan error,
) error {
temporaryTableId := fmt.Sprintf("LOAD_TEMP_%s_%s", p.tableId, uuid.New().String())
temporaryTable := p.client.Dataset(p.datasetId).Table(temporaryTableId)
if err := temporaryTable.Create(ctx, &bigquery.TableMetadata{
Schema: p.schema,
}); err != nil {
return err
}
defer func() {
if p.deleteTemporaryTable {
if err := temporaryTable.Delete(ctx); err != nil {
p.logger.Error(err, "failed to delete temporary table", "tableId", temporaryTable.TableID)
} else {
p.logger.Info("temporary table deleted", "tableId", temporaryTable.TableID)
}
}
}()
if err := p.waitUntilTableCreation(ctx, temporaryTableId); err != nil {
return err
}
p.logger.Info(fmt.Sprintf("created temporary table %v", temporaryTable.TableID))
loadedTotal := 0
temporaryJsonlFilePath := fmt.Sprintf("%v.jsonl.gz", temporaryTableId)
temporaryFile, err := os.CreateTemp("", temporaryJsonlFilePath)
if err != nil {
return fmt.Errorf("failed to create temporary file: %v", err)
}
defer func() {
if err := os.Remove(temporaryFile.Name()); err != nil {
p.logger.Error(err, "failed to remove temporary file", "path", temporaryFile.Name())
}
}()
temporaryFileGzipWriter := gzip.NewWriter(temporaryFile)
temporaryFileWriter := json.NewEncoder(temporaryFileGzipWriter)
loop:
for {
select {
case <-ctx.Done():
break loop
case msgs, ok := <-messages:
if !ok {
break loop
}
msgsSlice, ok := msgs.([]interface{})
if !ok {
errs <- fmt.Errorf("unexpected type: %T", reflect.TypeOf(msgs))
continue
}
for _, msg := range msgsSlice {
values, err := p.deserialize(msg)
if err != nil {
errs <- fmt.Errorf("failed to deserialize: %v, %v", msg, err)
continue
}
mp := map[string]interface{}{}
for i, v := range p.schema {
mp[v.Name] = values[i]
}
if err := temporaryFileWriter.Encode(mp); err != nil {
errs <- fmt.Errorf("failed to write to temporary file: %v, %v", values, err)
continue
}
}
if len(msgsSlice) > 0 {
loadedTotal += len(msgsSlice)
p.logger.Info(fmt.Sprintf("loaded %v rows", loadedTotal))
}
}
}
if err := temporaryFileGzipWriter.Close(); err != nil {
return fmt.Errorf("failed to close gzip writer: %v", err)
}
if err := temporaryFile.Close(); err != nil {
return fmt.Errorf("failed to close temporary file: %v", err)
}
p.logger.Info(fmt.Sprintf("loading into %v", temporaryTable.TableID))
temporaryFile, err = os.Open(temporaryFile.Name())
if err != nil {
return fmt.Errorf("failed to open temporary file: %v", err)
}
p.logger.Info(fmt.Sprintf("opened temporary file %v", temporaryFile.Name()))
reader, err := gzip.NewReader(temporaryFile)
if err != nil {
return fmt.Errorf("failed to create gzip reader: %v", err)
}
p.logger.Info(fmt.Sprintf("created gzip reader"))
source := bigquery.NewReaderSource(reader)
source.SourceFormat = bigquery.JSON
source.Schema = p.schema
loader := temporaryTable.LoaderFrom(source)
loader.WriteDisposition = bigquery.WriteTruncate
job, err := loader.Run(ctx)
if err != nil {
return fmt.Errorf("failed to load: %v", err)
}
status, err := job.Wait(ctx)
if err != nil {
return fmt.Errorf("failed to wait for job: %v", err)
}
if err := status.Err(); err != nil {
return fmt.Errorf("job failed: %v", err)
}
p.logger.Info(fmt.Sprintf("loaded into %v", temporaryTable.TableID))
// NOTE: CopierFrom is not supported by bigquery-emulator
// copier := p.client.Dataset(p.datasetId).Table(p.tableId).CopierFrom(temporaryTable)
copier := p.client.Query(fmt.Sprintf("SELECT * FROM `%v.%v`", temporaryTable.DatasetID, temporaryTable.TableID))
copier.WriteDisposition = bigquery.WriteTruncate
copier.Dst = p.client.Dataset(p.datasetId).Table(p.tableId)
job, err = copier.Run(ctx)
if err != nil {
return fmt.Errorf("failed to copy: %v", err)
}
status, err = job.Wait(ctx)
if err != nil {
return fmt.Errorf("failed to wait for job: %v", err)
}
if err := status.Err(); err != nil {
return fmt.Errorf("job failed: %v", err)
}
p.logger.Info(fmt.Sprintf("copied from %v to %v", temporaryTable.TableID, p.tableId))
return nil
}
type OutputPluginBigQueryConfig struct {
ProjectId string `yaml:"projectId"`
DatasetId string `yaml:"datasetId"`
TableId string `yaml:"tableId"`
Endpoint *string `yaml:"endpoint"`
Schema map[string]OutputPluginBigQueryConfigSchemaColumn `yaml:"schema"`
DeleteTemporaryTable *bool `yaml:"deleteTemporaryTable"`
}
type OutputPluginBigQueryConfigSchemaColumn struct {
Type string `yaml:"type"`
}
func NewOutputPluginBigQueryFromConfig(configYml []byte) (*OutputPluginBigQuery, error) {
var config OutputPluginBigQueryConfig
if err := yaml.Unmarshal(configYml, &config); err != nil {
return nil, err
}
options := []option.ClientOption{}
if config.Endpoint != nil {
options = append(options, option.WithEndpoint(*config.Endpoint))
}
client, err := bigquery.NewClient(context.Background(), config.ProjectId, options...)
if err != nil {
return nil, err
}
schema := bigquery.Schema{}
for name, column := range config.Schema {
t, err := getType(column.Type)
if err != nil {
return nil, err
}
schema = append(schema, &bigquery.FieldSchema{
Name: name,
Type: t,
})
}
deleteTemporaryTable := true
if config.DeleteTemporaryTable != nil {
deleteTemporaryTable = *config.DeleteTemporaryTable
}
return NewOutputPluginBigQuery(
client,
config.Endpoint,
config.DatasetId,
config.TableId,
schema,
func(item interface{}) ([]bigquery.Value, error) {
values := []bigquery.Value{}
for _, v := range schema {
values = append(values, item.(map[string]interface{})[v.Name])
}
return values, nil
},
deleteTemporaryTable,
), nil
}
func getType(t string) (bigquery.FieldType, error) {
switch strings.ToUpper(t) {
case "STRING":
return bigquery.StringFieldType, nil
case "INTEGER":
return bigquery.IntegerFieldType, nil
case "FLOAT":
return bigquery.FloatFieldType, nil
case "BOOLEAN":
return bigquery.BooleanFieldType, nil
case "TIMESTAMP":
return bigquery.TimestampFieldType, nil
case "RECORD":
return bigquery.RecordFieldType, nil
}
return "", errors.New("unknown type: " + t)
}