Apache Nifi:使用更新记录处理器替换列中的值 [英] Apache Nifi: Replacing values in a column using Update Record Processor
问题描述
我有一个csv,看起来像这样:
I have a csv, which looks like this:
name,code,age
Himsara,9877,12
John,9437721,16
Razor,232,45
我必须根据一些正则表达式替换 code
列.我的逻辑显示在下面的Scala代码中.
I have to replace the column code
according to some regular expressions. My logic is shown in a Scala code below.
if(str.trim.length == 9 && str.startsWith("369")){"PROB"}
else if(str.trim.length < 8){"SHORT"}
else if(str.trim.startsWith("94")){"LOCAL"}
else{"INT"}
我使用了 UpdateRecord
处理器替换了 code
列中的数据.我添加了一个名为/code
的属性,其中包含该值.
I used a UpdateRecord
Processor to replace the data in the code
column. I added a property called /code
which contains the value.
$ {field.value:replaceFirst('^ [0-9] {1,8} $','SHORT'):replaceFirst('[94] \ w +','OFF_NET')}
当用
替换代码时,此工作
- 长度小于8且带有"SHORT"
- 从94开始,以"LOCAL"
当在等于8位数字的
code
列中以 AND 开头时,我找不到替换列中的数据的方法.另外,如何替换数据如果没有出现上述情况.(应将数据替换为INT
的情况)
I am unable to find a way to replace data in the column,
code
when it's equal to 8 digits AND when it starts with 0. Also how can I replace the data if it doesn't fall into any condition mentioned above. (Situation which the data should be replaced withINT
)
希望您可以建议将一个工作流程或值添加到更新"记录中的属性,以进行上述两个替换.
Hope you can suggest a workflow or value to be added to the property in Update record to make the above two replacements happen.
推荐答案
有 length
和 startsWith
个函数.
${field.value:length():lt(8):ifElse(
'SHORT', ${field.value:startsWith(94):ifElse(
'LOCAL', ${field.value:length():equals(9):and(${field.value:startsWith(369)}):ifElse(
'PROB', 'INT'
)})})}
我已将换行符放在便于识别的功能上,但应将其删除.
I have put the line breaks for easy to recognize the functions but it should be removed.
顺便说一句,INT意味着要替换一些字符串值吗?抱歉造成混乱.
By the way, the INT means that some string values to replace? Sorry for the confusion.
好吧,如果只想使用正则表达式,则可以尝试以下代码.
Well, if you want to regular expression only, you can try the below code.
${field.value
:replaceFirst('[0-9]{1,8}', 'SHORT')
:replaceFirst('[94]\w+', 'OFF_NET')
:replaceFirst('369[0-9]{6}', 'PROB')
:replace(${field.value}, 'INT')
}
这篇关于Apache Nifi:使用更新记录处理器替换列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!