'NiFi EL How do I get a substring "up to" certain length?
I have this expression to print the first N chars of the flow file content. The problem is when the content length is less than N. What's a good way to say "up to first N chars". That is I will not get an index out of bounds error when the size is less than N.
${incoming_content.0:substring(0,100)}
Here my N=100
Thank you.
Solution 1:[1]
If the content of your flow files are not too big, you can use ExtractText to convert contents of flowfile as attribute and then use UpdateAttribute processor to run following logic
${incoming.0:length():le(100):ifElse(${incoming.0},${incoming.0:substring(0,100)})}
which is essentially checks if length of flowfile contents (in attribute) is less than equals to 100, if true, return entire string as is, else return substring up to 100 characters
Solution 2:[2]
I was also getting the index out of bound error, and hence created the below expression:
${firstName:length():gt(0):ifElse(${firstName:substring(0,5)},'')}
:length() => fetching the length of string ,
:gt(0) => return boolean true if length is greater than 0 ,
:ifElse => If true, then get first 5 characters, Else return empty string
I have tested the above expression having firstName attribute value as null, "" and "very long name".
Here is the link to Nifi Expression guide
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pushkr |
| Solution 2 |
