-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigquery.go
76 lines (62 loc) · 1.51 KB
/
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
package querier
import (
"context"
"cloud.google.com/go/bigquery"
"github.com/pkg/errors"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// BigQueryService expl
type BigQueryService struct {
Client *bigquery.Client
}
// NewBQ expl
func NewBQ(ctx context.Context, credPath string) (*BigQueryService, error) {
creds, err := getCreds(ctx, credPath)
if err != nil {
return nil, err
}
client, err := bigquery.NewClient(ctx, creds.ProjectID, option.WithCredentials(creds))
if err != nil {
return nil, err
}
return &BigQueryService{client}, nil
}
// DoQuery expl
func (*BigQueryService) DoQuery(ctx context.Context, query bigquery.Query, outChan chan<- []bigquery.Value, doneChan chan<- bool) error {
it, err := query.Read(ctx)
if err != nil {
return errors.Wrap(err, "While querying BigQuery")
}
for {
var values []bigquery.Value
err := it.Next(&values)
if err == iterator.Done {
doneChan <- true
break
}
if err != nil {
return errors.Wrap(err, "While reading query response")
}
outChan <- values
}
return nil
}
// GetQuota expl
func (*BigQueryService) GetQuota(ctx context.Context, query bigquery.Query) (bytes int64, err error) {
query.DryRun = true
query.Location = "EU"
job, err := query.Run(ctx)
if err != nil {
return
}
status := job.LastStatus()
if err != nil {
return
}
return status.Statistics.TotalBytesProcessed, nil
}
// MakeSQLquery expl
func (bq *BigQueryService) MakeSQLquery(query string) *bigquery.Query {
return bq.Client.Query(query)
}