登录
首页 >  Golang >  Go问答

使用Golang将数组数据插入BigQuery表格

来源:stackoverflow

时间:2024-02-08 12:06:22 129浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《使用Golang将数组数据插入BigQuery表格》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在尝试使用 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)

当我在 bigqyery 中查看 quality 列时,该列为 null。没有错误。该数组包含元素。 知道如何正确插入数组吗?我在文档中找不到任何内容。


正确答案


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

好了,本文到此结束,带大家了解了《使用Golang将数组数据插入BigQuery表格》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>