【问题标题】:Insert data to nested fields of a table at Google BigQuery在 Google BigQuery 中将数据插入表的嵌套字段
【发布时间】:2016-07-19 02:08:28
【问题描述】:

我在 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.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
}

【问题讨论】:

    标签: go google-bigquery


    【解决方案1】:

    您实际上需要构建一个与您的架构结构相匹配的对象结构。

    这里的困惑在于:

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

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您需要在客户端构造一个嵌套对象,而不是在 jsonRow 的键内使用点分符号。

      【讨论】:

      • Jeremy,你有任何代码示例或要点吗?这将非常有帮助
      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      相关资源
      最近更新 更多