我正在使用 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/