2017-06-22 11 views
1

jsonフォーマットのGoogle Cloud StorageファイルからBigQueryでテーブルを読み込み、BigQuery Console UIで有効になっているように 'スキーマの自動検出'オプションを有効にします。Cloud StorageからのJSONファイルのBiqQueryロードテーブルへの移動コード。自動検出スキーマ

GoでBigQueryパッケージcloud.google.com/go/bigqueryを使用していますが、ドキュメントからそれを把握することはできません。誰かがコードサンプルを提供できますか?私はPythonを使いたくない。

+0

私は 'Go'を知らないが、ロードの一例であるとしてフラグが、APIドキュメントにありファイル。だから、あなたはそれを得ることができるはずです。 https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/bigquery/file.go#L46 –

答えて

0

ありがとうございます。 FileConfig構造体はGCSReferenceの属性として設定する必要があります。

// Load JSON formatted files from 
 
// Google Cloud Storage to BigQuery 
 
// Auto-detect schema 
 
func LoadTblJson(ctx context.Context, client *bigquery.Client, 
 
\t pth string, datasetName string, tableName string) { 
 

 

 
\t dataset := client.Dataset(datasetName) 
 
\t gcsRef := bigquery.NewGCSReference(pth) 
 

 
\t // set FileConfig attribute of GCSReference struct 
 
\t var dataFormat bigquery.DataFormat 
 
\t dataFormat = "NEWLINE_DELIMITED_JSON" 
 
\t flConfig := bigquery.FileConfig{SourceFormat: dataFormat, 
 
\t \t AutoDetect: true, Schema: nil} 
 
\t gcsRef.FileConfig = flConfig 
 

 

 
\t loader := dataset.Table(tableName).LoaderFrom(gcsRef) 
 
\t loader.CreateDisposition = bigquery.CreateIfNeeded 
 
\t loader.WriteDisposition = bigquery.WriteEmpty 
 

 
\t job, err := loader.Run(ctx) 
 
\t if err != nil { 
 
\t \t //Handle 
 
\t } 
 
\t status, err := job.Wait(ctx) 
 
\t if err != nil { 
 
\t \t // Handle 
 
\t } 
 
\t if status.Err() != nil { 
 
\t \t //Handle 
 
\t } 
 

 

 

 
}