Himsara Gallege
Himsara Gallege

Reputation: 945

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

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.

Upvotes: 2

Views: 3037

Answers (1)

Lamanus
Lamanus

Reputation: 13591

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

Upvotes: 2

Related Questions