-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworker.go
161 lines (140 loc) · 4.33 KB
/
worker.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
package main
import (
"context"
"fmt"
"io"
"sort"
"strconv"
"strings"
"cloud.google.com/go/bigquery"
"github.com/dustin/go-humanize"
"github.com/jbsmith7741/uri"
"github.com/pcelvng/task"
"google.golang.org/api/option"
"github.com/pcelvng/task-tools/file"
"github.com/pcelvng/task-tools/file/buf"
)
type worker struct {
task.Meta
options
Destination `uri:"dest_table" required:"true"` // BQ table to load data into
File string `uri:"origin" required:"true"` // if not GCS ref must be file, can be folder (for GCS)
FromGCS bool `uri:"direct_load" default:"false"` // load directly from GCS ref, can use wildcards *
Truncate bool `uri:"truncate"` //remove all data in table before insert
Append bool `uri:"append"` // append data to table
DeleteMap map[string]string `uri:"delete"` // map of fields with value to check and delete
delete bool
}
func (o *options) NewWorker(info string) task.Worker {
w := &worker{
Meta: task.NewMeta(),
options: *o,
}
err := uri.Unmarshal(info, w)
if err != nil {
return task.InvalidWorker(err.Error())
}
// verify options
w.delete = len(w.DeleteMap) > 0
if w.delete && w.Truncate {
return task.InvalidWorker("truncate and delete options must be selected independently")
}
if !(w.delete || w.Truncate || w.Append) {
return task.InvalidWorker("insert rule required (append|truncate|delete)")
}
if w.delete {
w.Append = true
}
return w
}
func (w *worker) DoTask(ctx context.Context) (task.Result, string) {
opts := make([]option.ClientOption, 0)
if w.BqAuth != "" {
opts = append(opts, option.WithCredentialsFile(w.BqAuth))
}
client, err := bigquery.NewClient(ctx, w.Project, opts...)
if err != nil {
return task.Failf("bigquery client init %s", err)
}
//r, err := processFile(w.File, w.Fopts)
var loader *bigquery.Loader
if w.FromGCS { // load from Google Cloud Storage
gcsRef := bigquery.NewGCSReference(w.File)
gcsRef.SourceFormat = bigquery.JSON
loader = client.Dataset(w.Dataset).Table(w.Table).LoaderFrom(gcsRef)
} else { // load from file reader
r, err := file.NewReader(w.File, &w.Fopts)
if err != nil {
return task.Failf("problem with file: %s", err)
}
bqRef := bigquery.NewReaderSource(r)
bqRef.SourceFormat = bigquery.JSON
bqRef.MaxBadRecords = 1
loader = client.Dataset(w.Dataset).Table(w.Table).LoaderFrom(bqRef)
}
loader.WriteDisposition = bigquery.WriteAppend
if len(w.DeleteMap) > 0 {
q := delStatement(w.DeleteMap, w.Destination)
j, err := client.Query(q).Run(ctx)
if err != nil {
return task.Failf("delete statement: %s", err)
}
status, err := j.Wait(ctx)
if err != nil {
return task.Failf("delete wait: %s", err)
}
if status.Err() != nil {
return task.Failf("delete: %s", err)
}
status = j.LastStatus()
if qSts, ok := status.Statistics.Details.(*bigquery.QueryStatistics); ok {
w.SetMeta("rows_del", strconv.FormatInt(qSts.NumDMLAffectedRows, 10))
}
}
if w.Truncate {
loader.WriteDisposition = bigquery.WriteTruncate
}
job, err := loader.Run(ctx)
if err != nil {
return task.Failf("loader run: %s", err)
}
status, err := job.Wait(ctx)
if err == nil {
if status.Err() != nil {
return task.Failf("job completed with error: %v", status.Errors)
}
if sts, ok := status.Statistics.Details.(*bigquery.LoadStatistics); ok {
w.SetMeta("rows_insert", strconv.FormatInt(sts.OutputRows, 10))
return task.Completed("%d rows (%s) loaded", sts.OutputRows, humanize.Bytes(uint64(sts.OutputBytes)))
}
}
return task.Completed("completed")
}
func delStatement(m map[string]string, d Destination) string {
s := make([]string, 0)
for k, v := range m {
s = append(s, k+" = "+v)
}
sort.Sort(sort.StringSlice(s))
return fmt.Sprintf("delete from `%s.%s.%s` where %s", d.Project, d.Dataset, d.Table, strings.Join(s, " and "))
}
func processFile(path string, fOpts file.Options) (io.Reader, error) {
r, err := file.NewReader(path, &fOpts)
if err != nil {
return nil, err
}
writer, err := buf.NewBuffer(&buf.Options{UseFileBuf: true})
if err != nil {
return nil, err
}
scanner := file.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
writer.WriteLine([]byte(line))
}
if err := scanner.Err(); err != nil {
writer.Abort()
return nil, err
}
return writer, writer.Close()
}