Created 08-03-2021 12:01 AM
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.
Created on 08-05-2021 10:37 AM - edited 08-05-2021 10:39 AM
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:
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:
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.
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
Created on 08-09-2021 05:37 AM - edited 08-09-2021 05:38 AM
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
Created on 08-05-2021 10:37 AM - edited 08-05-2021 10:39 AM
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:
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:
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.
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
Created 08-06-2021 07:00 AM
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!
Created on 08-09-2021 05:37 AM - edited 08-09-2021 05:38 AM
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
Created 08-09-2021 07:15 AM