使用Golang将嵌套数据插入BigQuery

时间:2015-05-22 11:42:28

标签: go google-bigquery

我可以使用Golang将扁平对象插入BigQuery - 如何将嵌套数据插入表中?

我的BigQuery架构看起来像这样(来自示例):

[{
    "name": "kind",
    "mode": "nullable",
    "type": "string"
  },
  {
    "name": "fullName",
    "type": "string",
    "mode": "required"
  },
  { "name": "visit",
    "type": "record",
    "mode": "repeated",
    "fields": [
    {
       "name": "time",
       "type": "timestamp",
       "mode": "nullable"
    },
    {
       "name": "duration",
       "type": "integer",
       "mode": "nullable"
    }
   ]
  }
]

我的第一次插入尝试看起来像这样(例子):

func ExampleInsert(f string,) {

  jsonRow := make(map[string]bigquery.JsonValue)

  bq, _ := bigquery.New(client)
  request := new(bigquery.TableDataInsertAllRequest)

  rows := make([]*bigquery.TableDataInsertAllRequestRows, 1)

  jsonRow["kind"] = bigquery.JsonValue(kind)
  jsonRow["visit_duration"] = bigquery.JsonValue(duration)

  rows[i] = new(bigquery.TableDataInsertAllRequestRows)
  rows[i].Json = jsonRow

  bq.Tabledata.InsertAll(projectID, "visits", "visitsv4", request)
  ...
}

哪个扁平插入没有问题。我只是使用visit_duration

但是,我需要循环切片并添加到访问记录中。我已经尝试构建一个访问对象(没有要测试的循环)并将其添加到行但是它没有插入,我没有得到任何错误:

func ExampleInsert(f string,) {

  jsonRow := make(map[string]bigquery.JsonValue)

  bq, _ := bigquery.New(client)
  request := new(bigquery.TableDataInsertAllRequest)

  rows := make([]*bigquery.TableDataInsertAllRequestRows, 1)

  jsonRow["kind"] = bigquery.JsonValue(kind)

  visits := make([]*bigquery.TableDataInsertAllRequestRows, 1)

  jsonVisit := make(map[string]bigquery.JsonValue)
  jsonVisit["duration"] = rand.Intn(1000)
  visits[0] = new(bigquery.TableDataInsertAllRequestRows)
  visits[0].Json = jsonVisit

  jsonRow["visit"] = visits

  rows[i] = new(bigquery.TableDataInsertAllRequestRows)
  rows[i].Json = jsonRow

  bq.Tabledata.InsertAll(projectID, "visits", "visitsv4", request)

  _, err := Call.Do()
}

--- [溶液] ----

正如评论中所建议的那样,我也尝试创建一个切片,然后附加访问:

var visits []bigquery.JsonValue
visit := make(map[string]bigquery.JsonValue)
visit["duration"] = rand.Intn(100)
visits = append(visits, visit)

jsonRow["visit"] = visits

我可以确认这确实有效:)对于那些阅读本文的人来说,即使在添加切片之后它最初也不是因为我复制了表格。在这样做的过程中,我也将结果弄平了。小心。

1 个答案:

答案 0 :(得分:2)

访问应该是bigquery.JsonValue的一部分我不知道你为什么使用过:TableDataInsertAllRequestRows应该只使用一次有效载荷描述符。

var visits []bigquery.JsonValue
visit := make(map[string]bigquery.JsonValue)
visit["duration"] = rand.Intn(100)
visits = append(visits, visit)

jsonRow["visit"] = visits

PS。还要确保你的架构变平了