Golang BigQuery:将数组插入表中

huangapple go评论181阅读模式
英文:

Golang BigQuery: Insert array into table

问题

我正在尝试使用bigquery的golang库将数组插入到表中。

我有一个表模式,看起来像这样。数组是quality列,具有Repeated属性。

  1. tableSchema := bigquery.Schema{
  2. {Name: "issue_id", Type: bigquery.StringFieldType},
  3. {Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
  4. {Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
  5. }

这是我定义的记录类型:

  1. type escalationsRecord struct {
  2. IssueID bigquery.NullString `bigquery:"issue_id"`
  3. Quality []string `bigquery:"quality"`
  4. CreationTime bigquery.NullTimestamp `bigquery:"creation_date_ts"`
  5. }

这是我创建记录的方式:

  1. record := escalationsRecord{
  2. IssueID: bigquery.NullString{StringVal: fmt.Sprint(int(issue.Number)), Valid: true},
  3. Quality: qualityArray,
  4. CreationTime: bigquery.NullTimestamp{Timestamp: issue.CreatedAt.Time, Valid: true},
  5. }
  6. records = append(records, &record)

这是我将记录放入BigQuery的方式:

  1. inserter := table.Inserter()
  2. 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.

  1. tableSchema := bigquery.Schema{
  2. {Name: "issue_id", Type: bigquery.StringFieldType},
  3. {Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
  4. {Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
  5. }

This is how I defined the record:

  1. type escalationsRecord struct {
  2. IssueID bigquery.NullString `bigquery:"issue_id"`
  3. Quality []string `bigquery:"quality"`
  4. CreationTime bigquery.NullTimestamp `bigquery:"creation_date_ts"`
  5. }

This is how I create a record:

  1. record := escalationsRecord{
  2. IssueID: bigquery.NullString{StringVal: fmt.Sprint(int(issue.Number)), Valid: true},
  3. Quality: qualityArray,
  4. CreationTime: bigquery.NullTimestamp{Timestamp: issue.CreatedAt.Time, Valid: true},
  5. }
  6. records = append(records, &record)

This is how I put the records to BigQuery

  1. inserter := table.Inserter()
  2. 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()而不是自己定义模式。

所以,原来的代码是:

  1. tableSchema := bigquery.Schema{
  2. {Name: "issue_id", Type: bigquery.StringFieldType},
  3. {Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
  4. {Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
  5. }

现在的代码是:

  1. tableSchema, err := bigquery.InferSchema(escalationsRecord{})
  2. if err != nil {
  3. log.Fatal(err)
  4. }
英文:

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

  1. tableSchema := bigquery.Schema{
  2. {Name: "issue_id", Type: bigquery.StringFieldType},
  3. {Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
  4. {Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
  5. }

I now have

  1. tableSchema, err := bigquery.InferSchema(escalationsRecord{})
  2. if err != nil {
  3. log.Fatal(err)
  4. }

huangapple
  • 本文由 发表于 2022年11月18日 23:53:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/74492220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定