BigQuery存储编写API C#如何强制发送/序列化默认值



我使用的是BigQuery Storage Write API(C#net6(。BigQuery Storage Write API使用grpc/protobuf协议向BigQuery发送数据。

Protobuf(默认情况下(不序列化/发送默认值(比如0表示整数(,所以当我发送值为0的整数时,我在BigQuery上得到了null。

我的代码:

syntax = "proto3"
message SomeModel  {
int64 id = 1;
int64 quantity = 2;
}

为了将数据发送到BigQuery(使用Storage Write API(,我需要这样做:

var records = new List<SomeModel>();
records.Add(new SomeModel{Id = 1, Quantity = 0});
var protoData = new AppendRowsRequest.Types.ProtoData
{
WriterSchema = new ProtoSchema { ProtoDescriptor = SomeModel.Descriptor.ToProto() },
Rows = new ProtoRows 
{ 
SerializedRows = { records.Select(r => r.ToByteString()/*Serialization is made here*/ ) } 
}
};

当数据到达BigQuery表时,我得到了:

| id | quantity|
|--------------|
| 1  |  null   |

我想在BigQuery中存储quantity=0NOTquantity=null。

如何强制发送/序列化数量=0

或者同样的问题(更通用(:如何强制发送/序列化默认值

这可能与BigQuery Storage API期望协议缓冲区数据以proto2有线格式编码有关。

这是proto3的预期行为,因为一旦解析了消息,就无法判断字段是显式设置为默认值,还是根本没有设置。

Proto3并不打算取代proto2,它是作为无法使用proto2的用例和语言的替代品创建的,在可预见的未来,这两种语言版本都将得到支持。

我建议在这个问题上使用proto2。

最新更新