【问题标题】:BigQuery saves Float value 0.0 and Boolean false as nullBigQuery 将 Float 值 0.0 和 Boolean false 保存为 null
【发布时间】:2022-12-17 23:10:57
【问题描述】:

我正在使用云函数(python 3.10 运行时)在 protobuf 模式中接收和编码以下 JSON 有效负载,并发布到允许将数据处理到 BigQuery 的 PubSub 主题。

有效负载
{
  "data": [
    {
      "user_id": "XY25999A",
      "firstname": "John",
      "lastname": "Doe",
      "fee": 20.00,
      "is_active": false
    },
    {
      "user_id": "XY26999B",
      "firstname": "Sam",
      "lastname": "Foo",
      "fee": 0.00,
      "is_active": true
    },
    {
      "user_id": "XY27999C",
      "firstname": "Kay",
      "lastname": "Bent",
      "fee": 20.00,
      "is_active": true
    }
  ]
}
JSON模式
{
    "type":"object",
    "properties":{
       "user_id":{
          "type":"string"
       },
       "firstname":{
          "type":"string"
       },
       "lastname":{
          "type":"string"
       },
       "fee":{
          "type":"number"
       },
       "is_active":{
          "type":"boolean"
       }
    }
 }
protobuf 模式
message ProtoSchema {
    string user_id = 1;
    string firstname = 2;
    string lastname = 3;
    double fee = 4;
    bool is_active = 5;
  }

当数据处理到 BigQuery 时,活跃为了约翰费用为了山姆都分别显示null而不是false0.0

user_id firstname lastname fee is_active
XY25999A John Doe 20.00 null
XY26999B Sam Foo null true
XY27999C Kay Bent 20.00 true

这种行为有原因或解释吗?

【问题讨论】:

    标签: python types google-bigquery protocol-buffers jsonschema


    【解决方案1】:

    除非显式设置 required,否则 proto3 消息中的所有字段都是隐式的 optional。它有助于保存编码的消息大小。我猜想缺失字段的默认 bool 和 double 值分别为 false0 的云函数文档,如果字段具有默认值,则不会设置这些字段。因此,数据处理器应该为缺失的字段使用默认值。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-12
    • 2018-02-13
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多