如何在Jolt中按条件将已有值的字段设为null或空值?
解决Jolt中匹配customer_name后设置customer_location为null的问题
问题分析
你之前的Jolt规则失效原因如下:
modify阶段的isNull函数返回布尔值(true/false),而非你定义的NuLl字符串,导致后续Condition的拼接结果不符合预期,分支逻辑未触发。shift阶段同时保留了原始customer_location的赋值,且分支路径错误,无法覆盖原有字段值。
正确Jolt规则
假设你需要匹配的另一个表中的customer_name集合为["vivo", "MI"],可使用以下Jolt规则:
[ { "operation": "modify-overwrite-beta", "spec": { "*": { // 替换为你另一个表的customer_name列表 "targetNames": ["vivo", "MI"], "isMatch": "=contains(@(1,targetNames), @(1,customer_name))" } } }, { "operation": "shift", "spec": { "*": { "IN_num": "[&1].IN_num", "In_Details": "[&1].In_Details", "customer_name": "[&1].customer_name", "customer_ID": "[&1].customer_ID", "isMatch": { "true": { "@2,null": "[&3].customer_location" }, "false": { "@2,customer_location": "[&3].customer_location" } } } } }, { "operation": "remove", "spec": { "*": { "targetNames": "", "isMatch": "" } } } ]
规则说明
- modify-overwrite-beta阶段:
- 为每个元素添加
targetNames(待匹配的customer_name集合)和isMatch标志位,通过contains函数判断当前元素的customer_name是否在目标集合中,返回布尔值。
- 为每个元素添加
- shift阶段:
- 保留原有核心字段(
IN_num、In_Details、customer_name、customer_ID)。 - 根据
isMatch的值分支处理:匹配成功时将customer_location设为null,匹配失败则保留原始值。
- 保留原有核心字段(
- remove阶段:移除临时生成的
targetNames和isMatch字段,输出更简洁的结果。
测试输出
使用你提供的输入数据,执行上述规则后输出如下:
[ { "IN_num": "3", "In_Details": "404 error", "customer_name": "vivo", "customer_ID": "1003", "customer_location": null }, { "IN_num": "2", "In_Details": "404 error", "customer_name": "MI", "customer_ID": "1002", "customer_location": null }, { "IN_num": "1", "In_Details": "notworking", "customer_name": "Nokia", "customer_ID": "1001", "customer_location": "Hyd" } ]
内容的提问来源于stack exchange,提问作者sandilya ch
相关产品推荐
相关产品推荐

