How can I create a table in BigQuery with non-required fields?

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

How can I create a table in BigQuery with non-required fields?

问题

我正在使用Go语言编写一个RESTful API,用于在BigQuery中写入行。我正在使用Google BigQuery Go包

为了创建BigQuery模式,我正在根据一个结构体推断模式,就像示例中描述的那样。

问题是,生成的模式将所有非重复字段都标记为“必需”,因此,当我想上传包含空值的结构体时,空值会被上传为空字段...

这是我的结构体示例:

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: 处理错误。
}

如果我上传一个只设置了一个字段的结构体:

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

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

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

英文:

I'm making a RESTful API in Go that writes rows in BigQuery. I'm using Google BigQuery package for Go.

In order to create the BigQuery scheme, I'm infering the schema from a struct as described in the example.

The problem is that, the resulting schema has all the non-repeated fields as "Required", so that, when I want to upload a struct with null values, the null values are uploaded as empy fields...

This is an example of my struct:

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

This is an example of how the schema is created:

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

And, if I upload a struct with only one field set:

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

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

The result is that, in BigQuery, the fields "last_name" and "phone_number" is an empty string "" instead of NULL

答案1

得分: 2

为了实现这一点,显然你需要使用特定的BigQuery可空类型(NullInt64、NullFloat64、NullString、NullBool、NullTimestamp、NullDate、NullTime和NullDateTime)。

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

英文:

For that, apparently you would have to use the specific BigQuery nullable types (NullInt64, NullFloat64, NullString, NullBool, NullTimestamp, NullDate, NullTime and NullDateTime).

Example using bigquery.NullBool: https://godoc.org/cloud.google.com/go/bigquery#ex-InferSchema--Tags

答案2

得分: 0

我不是Go专家,但是看了一下这段代码(https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/bigquery/schema.go#L182),似乎inferFieldSchema方法总是将Required设置为true。我建议你提交一个bug报告来允许控制这个属性(虽然不清楚如何控制),或者在创建模式后对模式进行修补 - BigQuery支持将模式从REQUIRED修改为NULLABLE。

英文:

I am no Go expert, but looking at code at https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/bigquery/schema.go#L182 it looks like the inferFieldSchema method always sets Required: true. I would file a bug to allow to control it (although not clear how), or you can patch your schema after it was created - schema modification from REQUIRED to NULLABLE is supported by BigQuery.

huangapple
  • 本文由 发表于 2017年1月26日 22:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/41875878.html
匿名

发表评论

匿名网友

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

确定