【问题标题】:How can I create a table in BigQuery with non-required fields?如何在 BigQuery 中创建包含非必填字段的表?
【发布时间】:2017-06-12 01:36:26
【问题描述】:

我正在用 Go 制作一个 RESTful API,用于在 BigQuery 中写入行。我正在使用Google BigQuery package for Go

为了创建 BigQuery 方案,我从 example 中描述的结构推断架构。

问题在于,生成的架构将所有非重复字段都设为“必需”,因此,当我想上传具有空值的结构时,空值会作为空字段上传...

这是我的结构示例:

type Stats struct {
    Name            string        `bigquery:"name"`
    LastName        int           `bigquery:"last_name"`
    PhoneNumber     string        `bigquery:"phone_number"`
}

这是如何创建架构的示例:

testSchema, err := bigquery.InferSchema(Stats{})
if err != nil {
    // TODO: Handle error.
}

而且,如果我上传一个只有一个字段集的结构:

rows := []*Stats{
    {Name: "testA"},
}

u := table.Uploader()
err2 := u.Put(ctx, rows)

结果是,在 BigQuery 中,字段“last_name”和“phone_number”是空字符串“”而不是 NULL

【问题讨论】:

    标签: go google-bigquery


    【解决方案1】:

    我不是 Go 专家,但是在查看 https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/bigquery/schema.go#L182 的代码时,看起来 inferFieldSchema 方法总是设置 Required: true。我会提交一个错误以允许对其进行控制(虽然不清楚如何),或者您可以在创建架构后对其进行修补 - BigQuery 支持从 REQUIRED 到 NULLABLE 的架构修改。

    【讨论】:

      【解决方案2】:

      为此,显然您必须使用特定的 BigQuery 可空类型(NullInt64、NullFloat64、NullString、NullBool、NullTimestamp、NullDate、NullTime 和 NullDateTime)。

      使用 bigquery.NullBool 的示例:https://godoc.org/cloud.google.com/go/bigquery#ex-InferSchema--Tags

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-07
        • 2017-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-19
        • 2014-07-29
        • 1970-01-01
        相关资源
        最近更新 更多