Apache Nifi:使用更新记录处理器替换列中的值 [英] Apache Nifi: Replacing values in a column using Update Record Processor

查看:38
本文介绍了Apache Nifi:使用更新记录处理器替换列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 csv,如下所示:

姓名、代码、年龄Himsara,9877,12约翰,9437721,16剃刀,232,45

我必须根据一些正则表达式替换列code.我的逻辑显示在下面的 Scala 代码中.

if(str.trim.length == 9 && str.startsWith("369")){"PROB"}否则 if(str.trim.length < 8){"SHORT"}否则 if(str.trim.startsWith("94")){"LOCAL"}其他{INT"}

我使用了 UpdateRecord 处理器来替换 code 列中的数据.我添加了一个名为 /code 的属性,其中包含该值.

${field.value:replaceFirst('^[0-9]{1,8}$','SHORT'):replaceFirst('[94]\w+','OFF_NET')}

当用

替换代码时有效

  1. 长度小于 8 且带有SHORT"
  2. 以LOCAL"开头的 94

<块引用>

我无法找到一种方法来替换列中的数据,code 当它等于 8 位时 AND 当它以 0 开头时.另外我该如何替换数据如果它不属于上述任何条件.(数据应该被替换为INT的情况)

希望您能建议一个工作流或值添加到更新记录中的属性中,以实现上述两个替换.

解决方案

有一个 lengthstartsWith 函数.

${field.value:length():lt(8):ifElse('简短', ${field.value:startsWith(94):ifElse('本地', ${field.value:length():equals(9):and(${field.value:startsWith(369)}):ifElse('概率','INT')})})}

为了便于识别功能,我已经放置了换行符,但应该将其删除.

顺便说一下,INT的意思是要替换一些字符串值?抱歉造成混乱.

<小时>

好吧,如果你只想要正则表达式,你可以试试下面的代码.

${field.value:replaceFirst('[0-9]{1,8}', 'SHORT'):replaceFirst('[94]\w+', 'OFF_NET'):replaceFirst('369[0-9]{6}', 'PROB'):replace(${field.value}, 'INT')}

I have a csv, which looks like this:

name,code,age
Himsara,9877,12
John,9437721,16
Razor,232,45

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"}

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')}

This works when replacing code's with

  1. length less than 8 with "SHORT"
  2. starting with 94 with "LOCAL"

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 with INT)

Hope you can suggest a workflow or value to be added to the property in Update record to make the above two replacements happen.

解决方案

There is a length and startsWith functions.

${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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆