-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
201 lines (151 loc) · 4.66 KB
/
client.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
package bigquery
import (
"context"
"fmt"
"time"
googlebigquery "cloud.google.com/go/bigquery"
contractsv1 "github.com/JorritSalverda/jarvis-contracts-golang/contracts/v1"
"github.com/rs/zerolog/log"
)
// Client is the interface for connecting to bigquery
type Client interface {
CheckIfDatasetExists(ctx context.Context, dataset string) (exists bool)
CheckIfTableExists(ctx context.Context, dataset, table string) (exists bool)
CreateTable(ctx context.Context, dataset, table string, typeForSchema interface{}, partitionField string, waitReady bool) (err error)
UpdateTableSchema(ctx context.Context, dataset, table string, typeForSchema interface{}) (err error)
DeleteTable(ctx context.Context, dataset, table string) (err error)
InsertMeasurement(ctx context.Context, dataset, table string, measurement contractsv1.Measurement) (err error)
InitBigqueryTable(ctx context.Context, dataset, table string) (err error)
}
// NewClient returns new bigquery.Client
func NewClient(ctx context.Context, projectID string, enable bool) (Client, error) {
bigqueryClient, err := googlebigquery.NewClient(ctx, projectID)
if err != nil {
return nil, err
}
return &client{
projectID: projectID,
client: bigqueryClient,
enable: enable,
}, nil
}
type client struct {
projectID string
client *googlebigquery.Client
enable bool
}
func (c *client) CheckIfDatasetExists(ctx context.Context, dataset string) (exists bool) {
if !c.enable {
return false
}
ds := c.client.Dataset(dataset)
md, err := ds.Metadata(ctx)
log.Error().Err(err).Msgf("Error retrieving metadata for dataset %v", dataset)
return md != nil
}
func (c *client) CheckIfTableExists(ctx context.Context, dataset, table string) (exists bool) {
if !c.enable {
return false
}
tbl := c.client.Dataset(dataset).Table(table)
md, _ := tbl.Metadata(ctx)
// log.Error().Err(err).Msgf("Error retrieving metadata for table %v", table)
return md != nil
}
func (c *client) CreateTable(ctx context.Context, dataset, table string, typeForSchema interface{}, partitionField string, waitReady bool) (err error) {
if !c.enable {
return nil
}
tbl := c.client.Dataset(dataset).Table(table)
// infer the schema of the type
schema, err := googlebigquery.InferSchema(typeForSchema)
if err != nil {
return err
}
tableMetadata := &googlebigquery.TableMetadata{
Schema: schema,
}
// if partitionField is set use it for time partitioning
if partitionField != "" {
tableMetadata.TimePartitioning = &googlebigquery.TimePartitioning{
Field: partitionField,
}
}
// create the table
err = tbl.Create(ctx, tableMetadata)
if err != nil {
return err
}
if waitReady {
for {
if c.CheckIfTableExists(ctx, dataset, table) {
break
}
time.Sleep(time.Second)
}
}
return nil
}
func (c *client) UpdateTableSchema(ctx context.Context, dataset, table string, typeForSchema interface{}) (err error) {
if !c.enable {
return nil
}
tbl := c.client.Dataset(dataset).Table(table)
// infer the schema of the type
schema, err := googlebigquery.InferSchema(typeForSchema)
if err != nil {
return err
}
meta, err := tbl.Metadata(ctx)
if err != nil {
return err
}
update := googlebigquery.TableMetadataToUpdate{
Schema: schema,
}
if _, err := tbl.Update(ctx, update, meta.ETag); err != nil {
return err
}
return nil
}
func (c *client) DeleteTable(ctx context.Context, dataset, table string) (err error) {
if !c.enable {
return nil
}
tbl := c.client.Dataset(dataset).Table(table)
// delete the table
err = tbl.Delete(ctx)
if err != nil {
return err
}
return nil
}
func (c *client) InsertMeasurement(ctx context.Context, dataset, table string, measurement contractsv1.Measurement) (err error) {
if !c.enable {
return nil
}
tbl := c.client.Dataset(dataset).Table(table)
u := tbl.Uploader()
if err := u.Put(context.Background(), measurement); err != nil {
return err
}
return nil
}
func (c *client) InitBigqueryTable(ctx context.Context, dataset, table string) (err error) {
log.Debug().Msgf("Checking if table %v.%v.%v exists...", c.projectID, dataset, table)
tableExist := c.CheckIfTableExists(ctx, dataset, table)
if !tableExist {
log.Debug().Msgf("Creating table %v.%v.%v...", c.projectID, dataset, table)
err := c.CreateTable(ctx, dataset, table, contractsv1.Measurement{}, "MeasuredAtTime", true)
if err != nil {
return fmt.Errorf("Failed creating bigquery table: %w", err)
}
} else {
log.Debug().Msgf("Trying to update table %v.%v.%v schema...", c.projectID, dataset, table)
err := c.UpdateTableSchema(ctx, dataset, table, contractsv1.Measurement{})
if err != nil {
return fmt.Errorf("Failed updating bigquery table schema: %w", err)
}
}
return nil
}