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

时间:2017-11-03 18:53:39

标签: avro apache-nifi

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

架构摘要:

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

如果“personname”同时包含“first”和“last”,则它会起作用,但如果它只包含其中一个元素,则会失败,并显示错误:无法转换字段personname:无法解析union:{“last”:“ Smith“}不在”type“中:[”null“:,{”type“:”record“,”name“:”firstandorlast“,”fields“:[{”name“:”first“,”type“: [“null”,“string”]},{“name”:“last”,“type”:[“null”,“string”]}]}]

1 个答案:

答案 0 :(得分: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"
        }
      ]
  }
]