-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoperation.go
56 lines (49 loc) · 1.44 KB
/
operation.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
package bigquerysql
import (
"context"
"cloud.google.com/go/bigquery"
"github.com/pingcap/errors"
)
func runQuery(ctx context.Context, client *bigquery.Client, query string) error {
job, err := client.Query(query).Run(ctx)
if err != nil {
return errors.Trace(err)
}
status, err := job.Wait(ctx)
if err != nil {
return errors.Trace(err)
}
if status.Err() != nil {
return errors.Trace(errors.Errorf("Bigquery job completed with error: %v", status.Err()))
}
return nil
}
func deleteTable(ctx context.Context, client *bigquery.Client, datasetID, tableID string) error {
tableRef := client.Dataset(datasetID).Table(tableID)
err := tableRef.Delete(ctx)
if err != nil {
return errors.Trace(err)
}
return nil
}
func loadGCSFileToBigQuery(ctx context.Context, client *bigquery.Client, datasetID, tableID, gcsFilePath string) error {
gcsRef := bigquery.NewGCSReference(gcsFilePath)
gcsRef.SourceFormat = bigquery.CSV
gcsRef.NullMarker = "\\N"
gcsRef.AllowQuotedNewlines = true
gcsRef.PreserveASCIIControlCharacters = true
loader := client.Dataset(datasetID).Table(tableID).LoaderFrom(gcsRef)
loader.WriteDisposition = bigquery.WriteAppend
job, err := loader.Run(ctx)
if err != nil {
return errors.Trace(err)
}
status, err := job.Wait(ctx)
if err != nil {
return errors.Trace(err)
}
if status.Err() != nil {
return errors.Trace(errors.Errorf("Bigquery load snapshot job completed with error: %v", status.Err()))
}
return nil
}