-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (48 loc) · 1.37 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
package main
import (
"context"
"log"
"os"
"os/signal"
"time"
"cloud.google.com/go/bigquery"
"github.com/pkg/errors"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
projectID = "test-project"
timeout = 300
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
ctx, cancel := context.WithTimeout(ctx, time.Second*timeout)
defer cancel()
if err := run(ctx); err != nil {
log.Fatal(err)
}
log.Println("[INFO] process successfully finished")
}
func run(ctx context.Context) error {
addr := os.Getenv("BIGQUERY_EMULATOR_HOST")
if addr == "" {
return errors.New("error BIGQUERY_EMULATOR_HOST must not be empty")
}
// The following option imitates the one of Spanner emulator.
// https://github.com/googleapis/google-cloud-go/blob/5b307584fcd635635aae6a2fc4ba8252f2bbe22d/spanner/client.go#L177-L186
opts := []option.ClientOption{
option.WithEndpoint(addr),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication(),
internaloption.SkipDialSettingsValidation(),
}
cli, err := bigquery.NewClient(ctx, projectID, opts...)
if err != nil {
return errors.Wrap(err, "error NewClient")
}
log.Printf("[DEBUG] client created: %#v\n", cli)
return nil
}