英文:
Golang BigQuery: Insert array into table
问题
我正在尝试使用bigquery的golang库将数组插入到表中。
我有一个表模式,看起来像这样。数组是quality
列,具有Repeated
属性。
tableSchema := bigquery.Schema{
{Name: "issue_id", Type: bigquery.StringFieldType},
{Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
{Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
}
这是我定义的记录类型:
type escalationsRecord struct {
IssueID bigquery.NullString `bigquery:"issue_id"`
Quality []string `bigquery:"quality"`
CreationTime bigquery.NullTimestamp `bigquery:"creation_date_ts"`
}
这是我创建记录的方式:
record := escalationsRecord{
IssueID: bigquery.NullString{StringVal: fmt.Sprint(int(issue.Number)), Valid: true},
Quality: qualityArray,
CreationTime: bigquery.NullTimestamp{Timestamp: issue.CreatedAt.Time, Valid: true},
}
records = append(records, &record)
这是我将记录放入BigQuery的方式:
inserter := table.Inserter()
err2 := inserter.Put(b.ctx, records)
当我在BigQuery中查看quality
列时,它是NULL的。没有错误发生。数组中包含元素。
有没有办法正确插入数组?我在文档中找不到相关信息。
英文:
I'm trying to insert an array into a table using the bigquery golang library.
I have a table schema that looks like this. The array is the quality
column, has the Repeated
.
tableSchema := bigquery.Schema{
{Name: "issue_id", Type: bigquery.StringFieldType},
{Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
{Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
}
This is how I defined the record:
type escalationsRecord struct {
IssueID bigquery.NullString `bigquery:"issue_id"`
Quality []string `bigquery:"quality"`
CreationTime bigquery.NullTimestamp `bigquery:"creation_date_ts"`
}
This is how I create a record:
record := escalationsRecord{
IssueID: bigquery.NullString{StringVal: fmt.Sprint(int(issue.Number)), Valid: true},
Quality: qualityArray,
CreationTime: bigquery.NullTimestamp{Timestamp: issue.CreatedAt.Time, Valid: true},
}
records = append(records, &record)
This is how I put the records to BigQuery
inserter := table.Inserter()
err2 := inserter.Put(b.ctx, records)
The quality
column is NULL when I look at it in bigqyery. There are no errors. The array contains elements.
Any idea how to properly insert arrays? I can't find anything in the docs.
答案1
得分: 2
在他的评论中,BrianWagner提到的方法是有效的。我所做的唯一更改是使用InferSchema()
而不是自己定义模式。
所以,原来的代码是:
tableSchema := bigquery.Schema{
{Name: "issue_id", Type: bigquery.StringFieldType},
{Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
{Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
}
现在的代码是:
tableSchema, err := bigquery.InferSchema(escalationsRecord{})
if err != nil {
log.Fatal(err)
}
英文:
What @BrianWagner suggested in his comment works. The only change I did was to use the InferSchema()
instead of defining it myself.
So instead of
tableSchema := bigquery.Schema{
{Name: "issue_id", Type: bigquery.StringFieldType},
{Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
{Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
}
I now have
tableSchema, err := bigquery.InferSchema(escalationsRecord{})
if err != nil {
log.Fatal(err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论