forked from kikinteractive/go-bqstreamer
/
bqstreamer.go
57 lines (47 loc) · 1.47 KB
/
bqstreamer.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
// Package bqstreamer implements a concurrent stream (bulk) inserter to Google BigQuery.
package bqstreamer
import (
"io/ioutil"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
bigquery "google.golang.org/api/bigquery/v2"
)
const (
// Error channel buffer size.
errorBufferSize = 20
)
// A row associates a single table's row to its project, dataset and table.
type row struct {
projectID string
datasetID string
tableID string
data map[string]bigquery.JsonValue
}
// NewJWTConfig returns a new JWT configuration from a JSON key,
// acquired via https://console.developers.google.com.
//
// A config is used to authenticate with Google OAuth2.
func NewJWTConfig(keyPath string) (c *jwt.Config, err error) {
keyBytes, err := ioutil.ReadFile(keyPath)
if err != nil {
return
}
c, err = google.JWTConfigFromJSON(
keyBytes,
"https://www.googleapis.com/auth/bigquery")
// No need to check if err != nil since we return anyways.
return
}
// NewBigQueryService returns a new BigQuery service (client), authenticated via OAuth2/JWT.
//
// NOTE: This function authenticates with Google OAuth2 service,
// thus susceptible to network delays and blocks.
func NewBigQueryService(c *jwt.Config) (service *bigquery.Service, err error) {
// Create *http.Client.
client := c.Client(oauth2.NoContext)
// Create authenticated BigQuery service.
service, err = bigquery.New(client)
// No need to check if err != nil since we return anyways.
return
}