-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
48 lines (39 loc) · 1 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
package bq
import (
"context"
"cloud.google.com/go/bigquery"
"github.com/m-mizutani/goerr"
"github.com/m-mizutani/overseer/pkg/domain/model"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
type Client struct {
bq *bigquery.Client
}
func New(ctx context.Context, projectID string, options ...option.ClientOption) (*Client, error) {
bqClient, err := bigquery.NewClient(ctx, projectID, options...)
if err != nil {
return nil, goerr.Wrap(err, "Fail to create BigQuery client")
}
return &Client{
bq: bqClient,
}, nil
}
func (x *Client) Query(ctx context.Context, query string) ([]model.BigQueryRow, error) {
it, err := x.bq.Query(query).Read(ctx)
if err != nil {
return nil, goerr.Wrap(err, "Fail to query")
}
var itRows []model.BigQueryRow
for {
var row map[string]bigquery.Value
err := it.Next(&row)
if err == iterator.Done {
break
} else if err != nil {
return nil, goerr.Wrap(err, "Fail to get next row")
}
itRows = append(itRows, row)
}
return itRows, nil
}