Support Questions

Find answers, ask questions, and share your expertise

Using Apache Nifi How can you perform a mathematical operation on json attribute

avatar
Contributor

Hello, I want to perform a simple mathematical operation on a json attribute.
I simply want to subtract 30 from the value. 

Any help would be appreciated

7 REPLIES 7

avatar
Master Mentor

@fowler7878 

 

my initial thought here for you would be to chain the following three NiFi processor components together:
--> EvaluateJsonPath ---> UpdateAttribute ---> UpdateRecord -->

1. The EvaluateJsonPath processor should be configured to write the desired Json field value out to a NiFi FlowFileAttribute.
2. The UpdateAttribute processor would be used to manipulate that extracted value by subtracting 30 from it using the NiFi Expression Language (EL)
3. The UpdateRecord processor would be used to update your original FlowFile JSON content with the new Json field value.

Hope this helps you,

Matt

avatar
Contributor

Hey Matt, thank you so much
I've set up that flow but I'm having trouble on the actual updateattribute function I'm applying:
${COUNTFILES:minus(31)}

I'm using this and it's not modifying the value. 

Could you please help?

avatar
Master Mentor

@fowler7878 

 

Out of your EvaluateJsonPath processor your FlowFile now has an attribute with the name "COUNTFILES" and a numeric value?  You can confirm this by stopping the UpdateAttribute processor and allowing a test FlowFile to queue on the connection between EvaluateJsonPath and UpdateAttribute processors.  Then right click on connection and select "list queue" from the displayed context menu.  Then click on the "view details" icon to the far left of row for your listed FlowFile.  Within the FlowFile details UI you can inspect the attributes set on this FlowFile.

Then start only the UpdateAttribute processor while UpdateRecord is still stopped and repeat above to verify that updateAttribute processor modified the attribute value.

 

Matt

avatar
Contributor

So it's part of the flowfile content.  I attached the flow and the configs.
Sorry for the confusion if I caused any
I'm using a countnifierrro.PNG

 

nifi 1210.PNGnifierror1210.PNG

avatar
Master Mentor

@fowler7878 

 

Writing to the content will overwrite all the other content which is not what I think you want to be doing.  Plus the updateAttribute processor can only manipulate FlowFile attributes via the NiFi Expression Language (EL).

So you need to extract the json value to a FlowFile attribute in the EvaluateJsonPath processor.  Then manipulate that attributes value and then use UpdateRecord to update the actual content of your FlowFile using that FlowFile's attribute.

 

Matt

avatar
Contributor

I'm okay with writing over the content of the rest of the file.
I'm trying to output the rowcount results in a putemail so I'm okay with overwriting the rest of the flowfile. 
Can you still help with this?

avatar
Master Mentor

@fowler7878 

 

No problem.  At this point you have extracted the the count successfully and used updateAttribute processor to subtract 31 from it.  You do not need to write it back to the content to use it in putEmail processor.

 

The majority of the putEmail processor configuration properties support NiFi Expression Language also.
You can retrieve the value of your FlowFiiles attribute with this simple NiFi EL statement:

${COUNTFILES}

So you can use above anywhere in the string you configure in the the "Subject" and/or "Message" configuration properties.

Thanks,

Matt

If you found my answer help you to a solution, please don't forget to mark it as the accepted solution.