Support Questions

Find answers, ask questions, and share your expertise

Nifi Padding string ?

avatar

Is there a function or an expression in NIFI to left pad character strings

1 ACCEPTED SOLUTION

avatar
Master Mentor

@kannan chandrashekaran

You are correct that a function does not exist at this time for padding left or right of a string.

That being said, You can easily accomplish string padding using a simple flow consisting of a RouteOnAttribute" and "UpdateAttribute" processor.

14968-screen-shot-2017-05-01-at-115229-am.png

The RouteOnAttribute would contain one routing rule that checks the length of an attributes value and if it is not long enough routes it to the update attribute where you add one character of padding.

My rule looks like this:

14969-screen-shot-2017-05-01-at-115534-am.png

-- "10" is the length I want my attribute to be.

-- "test" is the attribute that I am calculating the length of.

My UpdateAttribute processor then simply pads the value assigned to"test" with a single character.

14970-screen-shot-2017-05-01-at-115826-am.png

FlowFiles will continue in this loop until the value of "test" has reached 10 charatcters in length.

Any FlowFile where the value associated to "test" has a length longer then 10 is just passed on without any change.

Thanks,

Matt

View solution in original post

3 REPLIES 3

avatar

Is there an standard padding function in NIFI to pad a string , as in leftpad or rightpad functions provided with other languages. I see toRadix come close to Leftpad for numbers resulting in the number getting padded with zeroes to the left.

avatar
Master Mentor

@kannan chandrashekaran

You are correct that a function does not exist at this time for padding left or right of a string.

That being said, You can easily accomplish string padding using a simple flow consisting of a RouteOnAttribute" and "UpdateAttribute" processor.

14968-screen-shot-2017-05-01-at-115229-am.png

The RouteOnAttribute would contain one routing rule that checks the length of an attributes value and if it is not long enough routes it to the update attribute where you add one character of padding.

My rule looks like this:

14969-screen-shot-2017-05-01-at-115534-am.png

-- "10" is the length I want my attribute to be.

-- "test" is the attribute that I am calculating the length of.

My UpdateAttribute processor then simply pads the value assigned to"test" with a single character.

14970-screen-shot-2017-05-01-at-115826-am.png

FlowFiles will continue in this loop until the value of "test" has reached 10 charatcters in length.

Any FlowFile where the value associated to "test" has a length longer then 10 is just passed on without any change.

Thanks,

Matt

avatar
Explorer

Anyone coming to look at this now (Jun 2022) - You can simply use UpdateAttribute with ${test:padLeft(10, '0')} to pad a string with zeroes.