我想从json格式的Google Cloud Storage文件中加载一个BigQuery表,并启用BigQuery控制台UI中的“自动检测模式”选项。
我想在Go中使用BigQuery包cloud.google.com/go/bigquery来做这件事,但从文档中找不出来。有人能提供一个代码样本吗?我不想使用Python。
发布于 2017-06-23 07:17:46
谢谢你的推荐。必须将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
}
}
https://stackoverflow.com/questions/44688080
复制相似问题