使用 Golang 将嵌套数据插入 BigQuery
Insert nested data into BigQuery using Golang
我可以使用 Golang 将平面对象插入 BigQuery - 如何将嵌套数据插入 table?
我的 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
但是,我需要遍历一个切片并添加到访问记录中。我试图构建一个 visit 对象(没有循环测试)并将其添加到该行但它没有插入并且我没有得到任何错误:
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
我可以确认这确实有效 :) 对于那些阅读本文的人来说,它最初不是的原因,即使在添加切片之后,是因为我复制了 table。在这样做的过程中,我也使结果变平了。小心。
访问次数应该是 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。还要确保你的架构扁平化
我可以使用 Golang 将平面对象插入 BigQuery - 如何将嵌套数据插入 table?
我的 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
但是,我需要遍历一个切片并添加到访问记录中。我试图构建一个 visit 对象(没有循环测试)并将其添加到该行但它没有插入并且我没有得到任何错误:
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
我可以确认这确实有效 :) 对于那些阅读本文的人来说,它最初不是的原因,即使在添加切片之后,是因为我复制了 table。在这样做的过程中,我也使结果变平了。小心。
访问次数应该是 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。还要确保你的架构扁平化