NiFi中如何根据JSON字段长度替换文本?
NiFi中实现logo字段的长度条件转换
需求说明
JSON数据流入NiFi后,需对logo字段执行以下逻辑:
- 若
logo字段值的长度超过10字符,将其替换为空字符串 - 若长度小于等于10字符,保留原字段值
优先使用JOLT转换处理器,也可采用其他替代方案。
JOLT转换方案
使用modify-overwrite-beta操作结合JOLT的cond函数实现条件判断,具体JOLT规范如下:
[ { "operation": "modify-overwrite-beta", "spec": { "logo": "=cond(length(@)>10, '', @)" } } ]
测试验证
输入1
{ "company_name": "mycompany", "companytyp": "producer", "Welcomemes": "welcome_message1", "logo": "This logo value is more than 10 characters " }
输出1
{ "company_name": "mycompany", "companytyp": "producer", "Welcomemes": "welcome_message1", "logo": "" }
输入2
{ "company_name": "mycompany", "companytyp": "producer", "Welcomemes": "welcome_message1", "logo": "short logo" }
输出2
{ "company_name": "mycompany", "companytyp": "producer", "Welcomemes": "welcome_message1", "logo": "short logo" }
替代方案:ExecuteScript处理器(Groovy脚本)
若JOLT版本不支持cond函数,可使用ExecuteScript处理器编写Groovy脚本实现逻辑:
import groovy.json.JsonSlurper import groovy.json.JsonBuilder def flowFile = session.get() if (!flowFile) return def slurper = new JsonSlurper() def json = slurper.parseText(flowFile.read().text) // 处理logo字段,空值判断避免空指针 if (json.logo != null && json.logo.length() > 10) { json.logo = "" } def builder = new JsonBuilder(json) flowFile.write(builder.toPrettyString()) session.transfer(flowFile, REL_SUCCESS)
内容的提问来源于stack exchange,提问作者Aditya
相关产品推荐
相关产品推荐

