Apache-nifi ,a short description about ifElse
I had been working with Apache-NiFi for last few months. During this period I mostly used csv
based operations.
Recently I got a problem with following scenario.
In my flowfile
, I need to do regex
operation on a column value and `replace` them with some other codes. I did this easily using replaceAll
function.
First lets see what replaceAll
does in Apache-NiFi. It takes two string arguments, first one is a regex
and second is the replacement value.
For more details, I highly recommend to read Apache-NiFi expression language guide’s replaceAll.
For the first attempt I have used following syntax for my column using UpdateRecord
processor :
${field.value:replaceAll(${field.value:length():le(8)},'Undefined') :replaceAll('[a-z]+','Wrong')}
Here what this does is, check whether length of the column value is less than 8 characters, if so it replaces that with Undefined
. But some data matched with [a-z]+
pattern had been replaced with Wrong
,even they are less than 8 characters.
I tried to solve this issue with myself and as I was taking too much time, I asked from StackOverflow community . Lamanus’s answer made me to think about ifElse
in Apache-NiFi.
So now lets go and see how Apache-NiFi’s ifElse
behaves. It takes two arguments, but the difference is that, its the result based on Subject
of the expression.
If the subject is true
then, first argument is evaluated else second.
As given in the example :${bool:ifElse('a','b')}
, here bool
has true. So if I use replaceAll
with this, value will replace with a.
In order to do my scenario using this, I had to change the logic. Checking whether it has more than 8 characters and then applying my custom regex
based operations.
${field.value:length():ge(8):ifElse(
${field.value:replaceAll(‘[a-z]+’,’Wrong’)
:replaceAll('[0-9][a-z]+','Variable1')
,${field.value:replace(${field.value},'Undefined')}
// this is the statement executed when value length is shorter than 8 characters.)}
I hope this short description will give you a better understanding how to use ifElse
in Apache-NiFi.