Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

IF statement for NiFi attribute result - NiFi

avatar
Contributor

Hello all

 

Within NiFi, updateAttribute processor I am trying to change an attribute called 'hive_database' based on the value of another attribute called 'hostname'.

 

For instance if:

#1: hostname = Mickey Mouse

#2: hostname = James Bond

 

I want to use a 'contains' statement (or similar ) to change the output of new attribute 'hive_database'

 

property:

hive_database

value:

${hostname:contains('Mickey'):<OUTPUT>('cartoon')} <OR>
${hostname:contains('Mickey'):<OUTPUT>('movie')}

 

So that #1 output would be: property= hive_database value= cartoon

 

The parts I'm stuck on is the OUTPUT and OR parts above, do you know if this would be possible using the update attribute processor? I am trying to avoid having to use routeOnAttribute to break out multiple different 'hosname' values to send data to the relevant 'hive_database' 

 

Hope this makes sense, unable to share screenshots etc as sensitive vales.

 

Any help greatly appreciated. 

1 ACCEPTED SOLUTION

avatar
Super Mentor

@Griggsy 

The "Advanced" UI capability in the UpdateAttribute processor allows you to setup rules to accomplish exactly what you are describing here in your use case.  When you right click on the UpdateAttribute processor you will see the "Advanced" button in the lower left corner.

From the Advanced UI you can configure conditional rules.
1. Click "+" to the right of "Rules" to create your first rule.
2. Then click "+" to right of "Conditions" to create your boolean NiFi Expression Language (NEL) statement(s).  When using multiple statements, all statements must result in true before the corresponding "Action(s)" would be applied.
3. Then click "+" to right of "Actions" to set create one or more actions that are applied if the condition(s) for this rule are true.

The thing is that your example or condition does not make sense to me.
You are returning the value from a FlowFile attribute named "hostname" and checking if it contains the string "Mickey" in both NEL statements.  However, for each you want to return a different output value? 
Since both would always return either "true" or "false", which output would be desired?

Here is example where i check if "Hostname" attribute contains "Mickey" and if that returns true I set FlowFile attribute "hive_database" to "cartoon".  I then have a second rule that checks the "Hostname" FlowFile attribute to see if it contains "Bond" and iid that returns true, I set "hive_database" FlowFile" attribute to "movie".

MattWho_0-1641822662960.png

The "conditions" and "actions" look similar for the "Bond" rule except "Mickey" is changed to "Bond" and cartoon is set to "movie" instead of "cartoon".

So above rules give you an If/then capability. Another feature of using the advanced UI is the "else" capability.  Outside the "Advanced" UI set a attribute for "hive_database":

MattWho_1-1641823020085.png


If I were to define actions that set the "hive_database" FlowFile attribute and upon evaluating a FlowFile none of the configured rules that set the "hive_database" attribute are applied, the update Attribute processor will set apply the value set outside the advanced UI.  If a rule sets the the attribute "hive_database", then value defined outside advanced UI is ignored/not set.

If you found this response assisted with your query, please take a moment to login and click on "Accept as Solution" below this post.

Thank you,

Matt





 

View solution in original post

5 REPLIES 5

avatar
Super Mentor

@Griggsy 

The "Advanced" UI capability in the UpdateAttribute processor allows you to setup rules to accomplish exactly what you are describing here in your use case.  When you right click on the UpdateAttribute processor you will see the "Advanced" button in the lower left corner.

From the Advanced UI you can configure conditional rules.
1. Click "+" to the right of "Rules" to create your first rule.
2. Then click "+" to right of "Conditions" to create your boolean NiFi Expression Language (NEL) statement(s).  When using multiple statements, all statements must result in true before the corresponding "Action(s)" would be applied.
3. Then click "+" to right of "Actions" to set create one or more actions that are applied if the condition(s) for this rule are true.

The thing is that your example or condition does not make sense to me.
You are returning the value from a FlowFile attribute named "hostname" and checking if it contains the string "Mickey" in both NEL statements.  However, for each you want to return a different output value? 
Since both would always return either "true" or "false", which output would be desired?

Here is example where i check if "Hostname" attribute contains "Mickey" and if that returns true I set FlowFile attribute "hive_database" to "cartoon".  I then have a second rule that checks the "Hostname" FlowFile attribute to see if it contains "Bond" and iid that returns true, I set "hive_database" FlowFile" attribute to "movie".

MattWho_0-1641822662960.png

The "conditions" and "actions" look similar for the "Bond" rule except "Mickey" is changed to "Bond" and cartoon is set to "movie" instead of "cartoon".

So above rules give you an If/then capability. Another feature of using the advanced UI is the "else" capability.  Outside the "Advanced" UI set a attribute for "hive_database":

MattWho_1-1641823020085.png


If I were to define actions that set the "hive_database" FlowFile attribute and upon evaluating a FlowFile none of the configured rules that set the "hive_database" attribute are applied, the update Attribute processor will set apply the value set outside the advanced UI.  If a rule sets the the attribute "hive_database", then value defined outside advanced UI is ignored/not set.

If you found this response assisted with your query, please take a moment to login and click on "Accept as Solution" below this post.

Thank you,

Matt





 

avatar
Contributor

Thanks Matt!! I hadn't used the advanced tab on the processor until now, worked a treat for my use case. Apologies for the slightly confusing question.

 

I am now able to pick out key words from the hostname attribute to direct them to the correct database. Thanks for the quick response!

avatar
Contributor

Hi, for me this is not working. My desired attribute remains unchanged after it undergoes the UpdateAttribute processor. What could be the cause of this? I have a property called "api" with the default value "web page". My rule is ${url:contains('webpage')}

and the action "api" and "web page". The "url" property contains the address as in "https://www.webpage.com/site".

Any ideas what I should check?

avatar
Contributor

Hi @Fredi 

Can you send a screenshot of the advanced tab rules if possible, I'm a little confused as to what you are wanting to achieve.

 

Cheers

avatar
Super Mentor

@Fredi 
A screenshot of the configuration of your UpdateAttribute processor including main configuration and configuration in the "Advanced" UI would be very helpful in understanding your setup and issue.

Thanks,

Matt