-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
67 lines (51 loc) · 1.66 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
package main
import (
"errors"
"strings"
tools "github.com/pcelvng/task-tools"
"github.com/pcelvng/task-tools/bootstrap"
"github.com/pcelvng/task-tools/file"
)
const (
taskType = "bq_load"
desc = `load a delimited json file into BigQuery
info params
- origin: (required) file to be loaded (gs://path/file.json)
- destination: (required) project.dataset.table to be insert into
- truncate: truncate the table (delete ALL and insert). Default behavior is to append data
- delete: map field defines the column and values to delete before inserting (delete=id:10|date:2020-01-02)
example for file reader:
{"task":"bq_load", "info":"gs://my/data.json?dest_table=project.reports.impressions&delete=date:2020-01-02|id:11"}
example for GCS reference:
{"task":"bq_load", "info":"gs://folder/*.json?dest_table=project.reports.impressions&from_gcs=true&append=true"}`
)
type options struct {
BqAuth string `toml:"bq_auth" comment:"file path to service file"`
Fopts file.Options `toml:"file"`
}
func (o *options) Validate() error {
return nil
}
func main() {
opts := &options{}
app := bootstrap.NewWorkerApp(taskType, opts.NewWorker, opts).
Description(desc).
Version(tools.Version).Initialize()
app.Run()
}
type Destination struct {
Project string
Dataset string
Table string
}
func (d *Destination) UnmarshalText(text []byte) error {
l := strings.Split(string(text), ".")
if len(l) != 3 || len(l[0]) == 0 || len(l[1]) == 0 || len(l[2]) == 0 {
return errors.New("requires (project.dataset.table)")
}
d.Project, d.Dataset, d.Table = l[0], l[1], l[2]
return nil
}
func (d Destination) String() string {
return d.Project + "." + d.Dataset + "." + d.Table
}