Apache Nifi: Replacing values in a column using Update Record Processor

廉价感情. 提交于 2021-01-28 04:21:37

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/59047663/apache-nifi-replacing-values-in-a-column-using-update-record-processor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!