首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >云存储JSON文件BiqQuery加载表Go代码;自动检测模式

云存储JSON文件BiqQuery加载表Go代码;自动检测模式
EN

Stack Overflow用户
提问于 2017-06-22 08:08:42
回答 1查看 615关注 0票数 1

我想从json格式的Google Cloud Storage文件中加载一个BigQuery表,并启用BigQuery控制台UI中的“自动检测模式”选项。

我想在Go中使用BigQuery包cloud.google.com/go/bigquery来做这件事,但从文档中找不出来。有人能提供一个代码样本吗?我不想使用Python。

EN

回答 1

Stack Overflow用户

发布于 2017-06-23 07:17:46

谢谢你的推荐。必须将FileConfig结构设置为GCSReference的属性:

代码语言:javascript
复制
// 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
	}



}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44688080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档