You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 04:42:38