在Google BigQuery中将数据插入表的嵌套字段
我在Cloud BigQuery中有一个表,但service.Tabledata.InsertAll调用会将数据插入嵌套字段在Google BigQuery中将数据插入表的嵌套字段,go,google-bigquery,Go,Google Bigquery,我在Cloud BigQuery中有一个表,但service.Tabledata.InsertAll调用会将数据插入嵌套字段 // works jsonRow["name"] = bigquery.JsonValue("Name") // doesn't work jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine") rows[index] = new(bigquery.TableDataInsertAll
// works
jsonRow["name"] = bigquery.JsonValue("Name")
// doesn't work
jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")
rows[index] = new(bigquery.TableDataInsertAllRequestRows)
rows[index].Json = jsonRow
insertRequest := &bigquery.TableDataInsertAllRequest{Rows: rows}
insertRequest.IgnoreUnknownValues = true
call := service.Tabledata.InsertAll(project, dataset, "analytics_events", insertRequest)
if res, err := call.Do(); err!=nil{
Log.Fatal("Unable to Insert to BigQuery ", err)
return err
}
您需要构建一个嵌套的对象客户端,而不是在jsonRow的键内使用点符号。您实际上需要构建一个与模式结构匹配的对象结构 这里的混乱之处在于:
jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")
不会创建您期望的对象结构。您创建的json对象实际上如下所示:
{
"geo_location.City.Names.en": "Irvine"
}
而你想要的东西看起来像:
{
"geo_location": {
"City": {
"Names": {
"en": "Irvine"
}
}
}
}
因此,您的代码应该如下所示:
// Probably not valid code. Just guessing.
jsonRow["geo_location"] = bigquery.JsonObject()
jsonRow["geo_location"]["City"] = bigquery.JsonObject()
jsonRow["geo_location"]["City"]["Names"] = bigquery.JsonObject()
jsonRow["geo_location"]["City"]["Names"]["en"] = bigquery.JsonValue("Irvine")
希望能有所帮助。Jeremy,您有任何代码示例或要点吗?那将非常有帮助