-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbqtable.go
65 lines (54 loc) · 1.99 KB
/
bqtable.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
// Package table provides functionality for converting empty BQ Table with provided AVRO Schema
package table
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"cloud.google.com/go/bigquery"
"github.com/go-syar/avro-schema-bq/schema"
"google.golang.org/api/option"
)
func CreateBQTableWithSA(projectID, datasetID, tableID, serviceAccount, schemaFilePath string) error {
// service account := "service-account.json"
if projectID == "" || datasetID == "" || tableID == "" || serviceAccount == "" || schemaFilePath == "" {
return fmt.Errorf("missing one of the required parameters")
}
ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID, option.WithCredentialsFile(serviceAccount))
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Read the contents of the Avro schema file from the specified path (schemaFilePath).
avroSchemaContent, err := ioutil.ReadFile(schemaFilePath)
if err != nil {
fmt.Println("Error reading Avro schema file:", err)
return err
}
var avroSchema map[string]interface{}
// Unmarshal the Avro schema content into a map structure (avroSchema map[string]interface{}).
err = json.Unmarshal(avroSchemaContent, &avroSchema)
if err != nil {
fmt.Println("Error parsing Avro schema:", err)
return err
}
// Convert the Avro schema (avroSchema map[string]interface{}) to BigQuery schema format (bqFields []*bigquery.FieldSchema).
bqFields, err := schema.ConvertAvroToBigQuery(avroSchema)
if err != nil {
fmt.Println("Error:", err)
return err
}
// Create BigQuery table metadata (metadata) with the converted schema (bqFields bigquery.Schema).
metadata := &bigquery.TableMetadata{
Schema: bqFields,
}
// Create a reference to the BigQuery table using the specified dataset (datasetID) and table ID (tableID).
tableRef := client.Dataset(datasetID).Table(tableID)
// Create the BigQuery table using the provided table metadata (metadata).
if err := tableRef.Create(ctx, metadata); err != nil {
return err
}
return nil
}