【问题标题】:Avro schema issue when record missing a field记录缺少字段时的 Avro 架构问题
【发布时间】:2018-04-16 14:14:41
【问题描述】:

我正在使用 NiFi (v1.2) 处理器 ConvertJSONToAvro。我无法解析仅包含“记录”类型中 2 个元素中的 1 个的记录。该元素也可以完全从数据中丢失。我的 Avro 架构不正确吗?

架构 sn-p:

"name": "personname",
"type": [
  "null":,
  {
    "type": "record",
    "name": "firstandorlast",
    "fields": [
        {
          "name": "first",
          "type": [
            "null",
            "string"
          ]
        },
        {
          "name": "last",
          "type": [
            "null",
            "string"
          ]
        }
      ]
  }
] 

如果“personname”同时包含“first”和“last”,它可以工作,但如果它只包含其中一个元素,则会失败并出现错误:Cannot convert field personname: cannot resolve union: {"last":" Smith"} 不在 "type": [ "null":, { "type": "record", "name": "firstandorlast", "fields": [ { "name": "first", "type": [ "null", "string" ] }, { "name": "last", "type": [ "null", "string" ] } ] } ]

【问题讨论】:

    标签: avro apache-nifi


    【解决方案1】:

    您缺少默认值

    https://avro.apache.org/docs/1.8.1/spec.html#schema_record

    你的架构应该是这样的

    "name": "personname",
    "type": [
      "null":,
      {
        "type": "record",
        "name": "firstandorlast",
        "fields": [
            {
              "name": "first",
              "type": [
                "null",
                "string"
              ],
              "default": "null"
            },
            {
              "name": "last",
              "type": [
                "null",
                "string"
              ],
              "default": "null"
            }
          ]
      }
    ] 
    

    【讨论】:

    • 我按照建议添加了 "default": "null" 并且确实允许解析记录,但是我收到(很多)警告:Avro: Invalid default for field first (or last) : "null" 不是 ["null","string"]
    • 将 "default": "null" 更改为 "default": null 仍然给出相同的警告。
    • 没关系,这是一个“红鲱鱼”,这个模式不是我收到警告的原因。是的,我觉得自己很笨。
    猜你喜欢
    • 1970-01-01
    • 2020-03-17
    • 2016-06-17
    • 2022-12-06
    • 1970-01-01
    • 2021-04-01
    • 2018-12-04
    • 2020-12-10
    • 1970-01-01
    相关资源
    最近更新 更多