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

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

Insert data to nested fields of a table at Google BigQuery

问题

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

// 可行
jsonRow["name"] = bigquery.JsonValue("Name")

// 不可行
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("无法插入到BigQuery", err)
    return err
}
英文:

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

// 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
}

答案1

得分: 1

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

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

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

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

{
  "geo_location.City.Names.en": "Irvine"
}

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

{
  "geo_location": {
    "City": {
      "Names": {
        "en": "Irvine"
      }
    }
  }
}

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

// 可能不是有效的代码,只是猜测。
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")

希望对你有所帮助。

英文:

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

The confusion here is that the line:

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:

{
  "geo_location.City.Names.en": "Irvine"
}

Whereas you want something that looks like:

{
  "geo_location": {
    "City": {
      "Names": {
        "en": "Irvine"
      }
    }
  }
}

So your code should look something like:

// 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")

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:

确定