Golang BigQuery:将数组插入表中

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

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

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:

确定