基于引用标识符而非索引的NiFi Jolt值设置需求
NiFi Jolt转换:通过EventCodePrefixId匹配对应Prefix值
正确的Jolt转换规则
[ { "operation": "shift", "spec": { "EventCodePrefixes": { "*": { "Prefix": "prefixMap.&1.Prefix", "Id": "prefixMap.&1.Id" } }, "EventCodes": "EventCodes", "EventCodePrefixes": "EventCodePrefixes" } }, { "operation": "modify-overwrite-beta", "spec": { "EventCodes": { "*": { "EventCodePrefix": "@(3,prefixMap.@(1,EventCodePrefixId).Prefix)" } } } }, { "operation": "remove", "spec": { "prefixMap": "" } } ]
规则说明
- Shift阶段:将
EventCodePrefixes数组转换为以Id为键的prefixMap临时对象,把分散的数组项变成可通过ID直接索引的结构,解决按ID匹配的核心需求。 - Modify阶段:遍历
EventCodes的每一项,通过@(1,EventCodePrefixId)获取当前项的ID值,再从prefixMap中取出对应的Prefix,赋值给EventCodePrefix字段。@(3,prefixMap...)中的层级索引3用于定位到根节点下的临时映射对象。 - Remove阶段:清理临时生成的
prefixMap,保证输出结构与原输入一致。
输入示例
{"EventCodes":[{"EventCodePrefixId":2},{"EventCodePrefixId":2}],"EventCodePrefixes":[{"Id":1,"Prefix":"AA"},{"Id":2,"Prefix":"BB"}]}
预期输出
{"EventCodes":[{"EventCodePrefixId":2,"EventCodePrefix":"BB"},{"EventCodePrefixId":2,"EventCodePrefix":"BB"}],"EventCodePrefixes":[{"Id":1,"Prefix":"AA"},{"Id":2,"Prefix":"BB"}]}
原错误规则问题分析
原规则中@(3,EventCodePrefixes[0].Prefix)是硬编码取数组第0项的Prefix,没有根据EventCodePrefixId做动态匹配,导致无论ID是多少都返回AA,无法实现按ID关联的需求。
内容的提问来源于stack exchange,提问作者Dalibor
相关产品推荐
相关产品推荐

