【问题标题】:In Avro why must we specify a "null" string for correct Enum types?在 Avro 中,为什么我们必须为正确的 Enum 类型指定一个“null”字符串?
【发布时间】:2021-09-09 09:43:37
【问题描述】:

我对 Avro 序列化完全陌生,我正试图弄清楚复杂类型是如何定义的。

我对 Avro 如何在 Java 中生成枚举感到困惑。

{
    "type":"record",
    "namespace":"com.example",
    "name": "Customer",
    "doc":"Avro Schema for our Customer",
    "fields":[
      {"name":"first_name","type":"string","doc":"First Name of Customer"},
      {"name":"last_name","type":"string","doc":"Last Name of Customer"},
      {"name":"automated_email","type":"boolean","doc":"true if the user wants marketing email", "default":true},
      {
        "name": "customer_type",
        "type": ["null",
          {
            "name": "Customertype",
            "type": "enum",
            "symbols": ["OLD","NEW"]
          }
        ]
      }
    ]
  }

注意customer_type 字段。如果我给出null,那么在我生成的源代码中,我会得到正确的Enum 类型,即:

private com.example.Customertype customer_type;

但是当我删除 null 值并以下列方式定义 customer_type 时:

{
        "name": "customer_type",
        "type": [
          {
            "name": "Customertype",
            "type": "enum",
            "symbols": ["OLD","NEW"]
          }
        ]
      }

声明更改为:

private Object customer_type;

那个“null”字符串是什么意思?为什么它很重要?

我已尝试查看 AVRO 规范,但没有任何东西给我一个明确的答案为什么会这样工作。

我正在使用 AVRO Maven 插件。

任何 AVRO 初学者资源也将不胜感激。

谢谢。

【问题讨论】:

    标签: avro avro-tools


    【解决方案1】:

    如果要删除null,则应删除[] 括号(因为它不再是联合)。

    所以您的 customer_type 架构应该如下所示:

    {
            "name": "customer_type",
            "type": {
              "name": "Customertype",
              "type": "enum",
              "symbols": ["OLD","NEW"]
            }
          }
    

    【讨论】:

    • 是的,但是为什么删除null会导致生成的对象中的类型为java.lang.Object
    猜你喜欢
    • 1970-01-01
    • 2016-11-06
    • 2014-08-28
    • 1970-01-01
    • 2019-08-27
    • 2022-06-15
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多