记录缺少字段时出现 Avro 架构问题

标签 avro apache-nifi

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

架构片段:

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

如果“personname”同时包含“first”和“last”,则它可以工作,但如果它只包含其中一个元素,则它会失败并出现错误:无法转换字段 personname:无法解析联合:

{ "last":"Smith" }

不在

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

最佳答案

您缺少默认值

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"
        }
      ]
  }
] 

关于记录缺少字段时出现 Avro 架构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47102681/

相关文章:

apache-nifi - 更新处理器返回空字符串 : Converting string date to long in NIFI

hadoop - Apache Nifi MergeContent 输出数据不一致?

Java PUT 请求 NiFi

mysql - 由于用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO)),Kylo data_ingest 提要作业失败

scala - 使用Flink中的RollingSink将用Avro序列化的对象写入HDFS [Scala]

Avro 模式演变

apache-kafka - 带有 kafka-avro-console-consumer 的未知魔法字节

java - Avro 字段名称以数字/整数开头

java - Avro Java SpecificDatumReaders 是否可重用?

apache-nifi - 在 Apache NiFi 中,是否可以增加/减少 Controller 的范围?