Search code examples
apache-nifiminify

Nifi - Converting previously saved now() in attribute


I use UpdateAttribute to check timings of slow processors. First UpdateAttribute has DATA_START ${now()}, while the second one has DATA_STOP ${now()} attribute. Slow processor is placed between the two.

I've been trying to convert DATA_START to a number, so I could calculate the time difference.

${now():toNumber():minus(${DATA_START:toNumber()}):format("HH:mm:ss")}

This doesn't work, unfortunately. The issue is in this part ${DATA_START:toNumber()}. NiFi couldn't convert previously saved now() into a number.

I have a lot of timers in my flow, so adding new attributes that will contain ${now():toNumber()} would be troublesome.

Is there any way to apply toNumber() to a previously saved now()?

Various types of format and toDate() didn't work.


Solution

  • I found a solution, but it looks like a hack

    ${now():toNumber():minus(${DATA_START:format('yyyy-MM-dd HH:mm:ss'):toDate('yyyy-MM-dd HH:mm:ss'):toNumber()}):divide(1000)}

    For some reason, NiFi doesn't think that previously saved now() is a Data. I convert it to a String, then convert it back to a Date, and in the end, I could get a timestamp. Timestamps can be manipulated and that's what I need.

    ${DATA_START:format('yyyy-MM-dd HH:mm:ss'):toDate('yyyy-MM-dd HH:mm:ss'):toNumber()}