【问题标题】:Go code for BiqQuery load table of JSON files from Cloud Storage; auto-detect schema从 Cloud Storage 加载 JSON 文件的 BigQuery 表的 Go 代码;自动检测架构
【发布时间】:2017-06-22 00:08:41
【问题描述】:
我想从 json 格式的 Google Cloud Storage 文件加载 BigQuery 中的表,并启用 BigQuery 控制台 UI 中可用的“自动检测架构”选项。
我想在 Go 中使用 BigQuery 包 cloud.google.com/go/bigquery执行此操作,但无法从文档中弄清楚。有人可以提供代码示例吗?我不想使用 Python。
【问题讨论】:
标签:
json
go
google-bigquery
【解决方案1】:
感谢您的参考。 FileConfig 结构必须设置为 GCSReference 的属性:
// Load JSON formatted files from
// Google Cloud Storage to BigQuery
// Auto-detect schema
func LoadTblJson(ctx context.Context, client *bigquery.Client,
pth string, datasetName string, tableName string) {
dataset := client.Dataset(datasetName)
gcsRef := bigquery.NewGCSReference(pth)
// set FileConfig attribute of GCSReference struct
var dataFormat bigquery.DataFormat
dataFormat = "NEWLINE_DELIMITED_JSON"
flConfig := bigquery.FileConfig{SourceFormat: dataFormat,
AutoDetect: true, Schema: nil}
gcsRef.FileConfig = flConfig
loader := dataset.Table(tableName).LoaderFrom(gcsRef)
loader.CreateDisposition = bigquery.CreateIfNeeded
loader.WriteDisposition = bigquery.WriteEmpty
job, err := loader.Run(ctx)
if err != nil {
//Handle
}
status, err := job.Wait(ctx)
if err != nil {
// Handle
}
if status.Err() != nil {
//Handle
}
}