如何在Apache NiFi中用Jolt规范拆分JSON数组并处理InvoiceMonth
Jolt规范实现多数组字段JSON拆分需求
需求说明
当credit对象包含多字段数组时,需将原JSON拆分为多个子JSON:
- 第一个子JSON保留原
InvoiceMonth值 - 其余子JSON的
InvoiceMonth值设为"0" - 每个子JSON的
credit对象对应原数组中对应索引的单个值
Jolt转换规范
[ { "operation": "shift", "spec": { "*": { "credit": { "*": { "*": { "@(3,InvoiceMonth)": "[&1].InvoiceMonth", "@(2,resource_name)": "[&1].resource_name", "@(2,resource_global_name)": "[&1].resource_global_name", "@(2,label)": "[&1].label", "@": "[&1].credit.&(2)" } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "InvoiceMonth": ["=notEquals(@(1), 0)", "@(0)", "0"] } } } ]
输入示例
[ { "InvoiceMonth": "202306", "credit": { "name": [ "7828credit:0", "2738credit:0" ], "amount": [ "2.5E-5", "5.0E-5" ], "full_name": [ "Committed USAGE", "Committed USAGE " ], "id": [ "7822credit:0", "2732credit:0" ], "type": [ "COMMITTEDDISCOUNT", "COMMITTEDDISCOUNTBASE" ] }, "resource_name": "projects-2", "resource_global_name": "//https.googleapis.com/", "label": "" } ]
输出示例
[ { "InvoiceMonth": "202306", "credit": { "name": "7828credit:0", "amount": "2.5E-5", "full_name": "Committed USAGE", "id": "7822credit:0", "type": "COMMITTEDDISCOUNT" }, "resource_name": "projects-2", "resource_global_name": "//https.googleapis.com/", "label": "" }, { "InvoiceMonth": "0", "credit": { "name": "2738credit:0", "amount": "5.0E-5", "full_name": "Committed USAGE ", "id": "2732credit:0", "type": "COMMITTEDDISCOUNTBASE" }, "resource_name": "projects-2", "resource_global_name": "//https.googleapis.com/", "label": "" } ]
规范说明
- Shift操作:遍历
credit下的所有数组字段,按索引将对应值分配到新的数组对象中,同时携带resource_name、resource_global_name等非credit字段的值。 - Modify操作:通过索引判断,第一个子JSON(索引0)保留原
InvoiceMonth值,其余子JSON将InvoiceMonth覆盖为"0"。
内容的提问来源于stack exchange,提问作者pandu
相关产品推荐
相关产品推荐

