为了账号安全,请及时绑定邮箱和手机立即绑定

将数据插入到 Google BigQuery 表的嵌套字段中

将数据插入到 Google BigQuery 表的嵌套字段中

Go
小唯快跑啊 2021-12-27 18:25:53
我在 Cloud BigQuery 中有一个表,但service.Tabledata.InsertAll调用确实将数据插入到嵌套字段中。// works jsonRow["name"] = bigquery.JsonValue("Name")// doesn't workjsonRow["geo_location.City.Names.en"] = bigquery.JsonValue("Irvine")rows[index] = new(bigquery.TableDataInsertAllRequestRows)rows[index].Json = jsonRowinsertRequest := &bigquery.TableDataInsertAllRequest{Rows: rows}insertRequest.IgnoreUnknownValues = truecall := 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}
查看完整描述

2 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

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


这里的困惑在于该行:


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

希望有帮助。


查看完整回答
反对 回复 2021-12-27
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

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


查看完整回答
反对 回复 2021-12-27
  • 2 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信