Support Questions

Find answers, ask questions, and share your expertise

Modification of Attribute

avatar
Contributor

I have to extract the first character from each string provided in the Flowfile attribute.

Suppose I have one attribute as:

KeyValue : Nifi Test Project

Expected output

Key1: NTP

 

Please let me know how to get this and which processor to be used.

 

Thanks in advance.

2 ACCEPTED SOLUTIONS

avatar
Master Mentor

@midee 

 

Assuming you know how many space delimited strings you have, you could use the following NiFi Expression Language (NEL) statement to extract the first character from each string:

 

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}${attr1:getDelimitedField(3,' '):substring(0,1)}

 

 

 Above would take your example "Nifi Test Project" and produce "NTP".

If this was not a static number of strings, you would need to count the number of strings and then use the Advanced UI of the updateAttribute processor to execute the appropriate NEL statement depending on calculated number of strings in your source attribute

Here is an example UpdateAttribute processor configuration to accomplish this. Select "Configure" on the UpdateAttribute processor:

MattWho_1-1628184459086.png

In the lower left you will see button to enter the "ADVANCED" UI.
Within that UI you will create a new RULE for each  string count:

MattWho_2-1628184574073.png

You then create a "Conditions" which must resolve to "TRUE" before the "Actions" are applied.
above image handles when the subject attribute value contains a single string. Below is example of more complicated Action when string count is equal to 4.

MattWho_3-1628184786119.png

Adding more rules is as simple as copying the last rule and editing the Condition to increment equals value and add and additional "${attr1:getDelimitedField(5,' '):substring(0,1)}" to the end with the next incremented field count.


If you found this addressed your query, please take a moment to login and click "Accept as Solution".
Thank you,

Matt

View solution in original post

avatar
Master Mentor

@midee 

 

The condition NEL statement and resulting actions NEL statements will be unique for each rule you create.

For example:
Rule 1:
Conditions: 

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(1)}

 

Actions:

 

${attr1:substring(0,1)}

 


Rule2:
Conditions:

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(2)}

 

Actions:

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}

 


Rule3:
Conditions:

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(3)}

 

Actions:

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}${attr1:getDelimitedField(3,' '):substring(0,1)}

 

 

Rule4:
Conditions:

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(4)}

 

Actions:

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}${attr1:getDelimitedField(3,' '):substring(0,1)}${attr1:getDelimitedField(4,' '):substring(0,1)}

 


You will notice in the Conditions NEL statement, the last number is incrementing for each rule and in the Actions NEL statement the statement gets a bit longer with each additional string 

If you found these answers to your query were useful, please take a moment to login and click "Accept as Solution" on all answers that helped.
Hope this helps,
Matt

View solution in original post

4 REPLIES 4

avatar
Master Mentor

@midee 

 

Assuming you know how many space delimited strings you have, you could use the following NiFi Expression Language (NEL) statement to extract the first character from each string:

 

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}${attr1:getDelimitedField(3,' '):substring(0,1)}

 

 

 Above would take your example "Nifi Test Project" and produce "NTP".

If this was not a static number of strings, you would need to count the number of strings and then use the Advanced UI of the updateAttribute processor to execute the appropriate NEL statement depending on calculated number of strings in your source attribute

Here is an example UpdateAttribute processor configuration to accomplish this. Select "Configure" on the UpdateAttribute processor:

MattWho_1-1628184459086.png

In the lower left you will see button to enter the "ADVANCED" UI.
Within that UI you will create a new RULE for each  string count:

MattWho_2-1628184574073.png

You then create a "Conditions" which must resolve to "TRUE" before the "Actions" are applied.
above image handles when the subject attribute value contains a single string. Below is example of more complicated Action when string count is equal to 4.

MattWho_3-1628184786119.png

Adding more rules is as simple as copying the last rule and editing the Condition to increment equals value and add and additional "${attr1:getDelimitedField(5,' '):substring(0,1)}" to the end with the next incremented field count.


If you found this addressed your query, please take a moment to login and click "Accept as Solution".
Thank you,

Matt

avatar
Contributor

Hi @MattWho ,

Thanks for your reply. 
However, when I put the expression mentioned in your reply under condition block. It throws error.

 

Could you please provide the expression mentioned under "Conditions" .

 

thanks!

avatar
Master Mentor

@midee 

 

The condition NEL statement and resulting actions NEL statements will be unique for each rule you create.

For example:
Rule 1:
Conditions: 

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(1)}

 

Actions:

 

${attr1:substring(0,1)}

 


Rule2:
Conditions:

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(2)}

 

Actions:

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}

 


Rule3:
Conditions:

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(3)}

 

Actions:

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}${attr1:getDelimitedField(3,' '):substring(0,1)}

 

 

Rule4:
Conditions:

 

${attr1:trim():length():minus(${attr1:trim():replaceAll(' ',''):length()}):plus(1):equals(4)}

 

Actions:

 

${attr1:substring(0,1)}${attr1:getDelimitedField(2,' '):substring(0,1)}${attr1:getDelimitedField(3,' '):substring(0,1)}${attr1:getDelimitedField(4,' '):substring(0,1)}

 


You will notice in the Conditions NEL statement, the last number is incrementing for each rule and in the Actions NEL statement the statement gets a bit longer with each additional string 

If you found these answers to your query were useful, please take a moment to login and click "Accept as Solution" on all answers that helped.
Hope this helps,
Matt

avatar
Contributor

@MattWho 

Thanks for providing the solutions .

 

thanks!