在Google BigQuery中向表的嵌套字段插入数据。

huangapple go评论119阅读模式
英文:

Insert data to nested fields of a table at Google BigQuery

问题

我在Cloud BigQuery中有一张表,但是service.Tabledata.InsertAll调用并没有将数据插入到嵌套字段中。

  1. // 可行
  2. jsonRow["name"] = bigquery.JsonValue("Name")
  3. // 不可行
  4. jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")
  5. rows[index] = new(bigquery.TableDataInsertAllRequestRows)
  6. rows[index].Json = jsonRow
  7. insertRequest := &bigquery.TableDataInsertAllRequest{Rows: rows}
  8. insertRequest.IgnoreUnknownValues = true
  9. call := service.Tabledata.InsertAll(project, dataset, "analytics_events", insertRequest)
  10. if res, err := call.Do(); err != nil {
  11. Log.Fatal("无法插入到BigQuery", err)
  12. return err
  13. }
英文:

I have a table in Cloud BigQuery, but the service.Tabledata.InsertAll call does insert data to the nested fields.

  1. // works
  2. jsonRow["name"] = bigquery.JsonValue("Name")
  3. // doesn't work
  4. jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")
  5. rows[index] = new(bigquery.TableDataInsertAllRequestRows)
  6. rows[index].Json = jsonRow
  7. insertRequest := &bigquery.TableDataInsertAllRequest{Rows: rows}
  8. insertRequest.IgnoreUnknownValues = true
  9. call := service.Tabledata.InsertAll(project, dataset, "analytics_events", insertRequest)
  10. if res, err := call.Do(); err!=nil{
  11. Log.Fatal("Unable to Insert to BigQuery ", err)
  12. return err
  13. }

答案1

得分: 1

实际上,你需要构建一个与你的模式结构相匹配的对象结构。

混淆的地方在于这一行代码:

  1. jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")

它并没有创建你期望的对象结构。你实际上创建的 JSON 对象看起来像这样:

  1. {
  2. "geo_location.City.Names.en": "Irvine"
  3. }

而你想要的是这样的结构:

  1. {
  2. "geo_location": {
  3. "City": {
  4. "Names": {
  5. "en": "Irvine"
  6. }
  7. }
  8. }
  9. }

所以你的代码应该像这样:

  1. // 可能不是有效的代码,只是猜测。
  2. jsonRow["geo_location"] = bigquery.JsonObject()
  3. jsonRow["geo_location"]["City"] = bigquery.JsonObject()
  4. jsonRow["geo_location"]["City"]["Names"] = bigquery.JsonObject()
  5. jsonRow["geo_location"]["City"]["Names"]["en"] = bigquery.JsonValue("Irvine")

希望对你有所帮助。

英文:

You actually need to construct an object structure that matches the structure of your schema.

The confusion here is that the line:

  1. jsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")

Doesn't create an object structure that you're expecting. The json object you've made actually looks like this:

  1. {
  2. "geo_location.City.Names.en": "Irvine"
  3. }

Whereas you want something that looks like:

  1. {
  2. "geo_location": {
  3. "City": {
  4. "Names": {
  5. "en": "Irvine"
  6. }
  7. }
  8. }
  9. }

So your code should look something like:

  1. // Probably not valid code. Just guessing.
  2. jsonRow["geo_location"] = bigquery.JsonObject()
  3. jsonRow["geo_location"]["City"] = bigquery.JsonObject()
  4. jsonRow["geo_location"]["City"]["Names"] = bigquery.JsonObject()
  5. jsonRow["geo_location"]["City"]["Names"]["en"] = bigquery.JsonValue("Irvine")

Hope that helps.

答案2

得分: 0

你需要在客户端构建一个嵌套对象,而不是在jsonRow的键中使用点符号表示法。

英文:

You need to construct a nested object client-side, instead of using dotted notation inside the key for your jsonRow.

huangapple
  • 本文由 发表于 2016年3月31日 04:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/36319549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定