Apache NiFi中XML解析异常:subevent数组index字段为null的解决方法
Apache NiFi XMLReader解析XML属性为null的解决方法
问题核心在于XMLReader默认仅解析XML元素的文本内容,不会自动识别元素属性,因此index属性未被映射到Avro字段中,以下是具体解决步骤:
调整Avro Schema的属性映射规则
必须在Avro Schema中为index字段添加xml.attribute注解,明确告知XMLReader该字段对应XML元素的属性。假设原XML结构如下:- 示例内容
对应的Avro Schema示例:
{ "type": "record", "name": "SubeventRecord", "fields": [ { "name": "item", "type": { "type": "record", "name": "ItemRecord", "fields": [ { "name": "index", "type": "int", "xml.attribute": "" }, { "name": "content", "type": "string", "xml.element": "text()" } ] } } ] }注解说明:
xml.attribute为空字符串时,默认匹配与字段名同名的属性;若属性名和字段名不一致,可填写具体属性名(如xml.attribute="item-index")。检查XMLReader的基础配置
- 确认
Root Element设置正确,需指向包含subevent的顶层节点(若单独处理subevent节点,直接设为subevent即可)。 - 若原XML带有命名空间,需在XMLReader的
Namespaces配置项中添加对应的命名空间映射,否则属性无法被识别。
- 确认
验证字段类型匹配
确认Avro Schema中index字段的类型与XML属性的实际类型一致:若XML中index是数字,Schema用int/long;若是字符串则用string,类型不匹配会导致解析结果为null。用小样本测试配置
提取仅包含subevent和item节点的最小XML样本,单独通过ConvertRecord测试,排除其他节点干扰,确认配置生效后再处理完整数据。
内容的提问来源于stack exchange,提问作者Mark Avreliy
相关产品推荐
相关产品推荐

