Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

Resolving date difference on Apache NIFI

avatar
Frequent Visitor

Good afternoon all, and thank you in advance.

I have a Apache NIFI solution that is pulling from API a pair of dates, i've grabbed out the relevant bit from the json:

"arrival" : "2023-07-24T14:33:00.263Z", "departure" : "2023-07-24T14:21:15.263Z"

And i'm successfully getting those values down to attributes via extract attributes.

However when i perform a toNumber() on them to convert to miliseconds since EPOCH to determine the time between the dates, i return an empty condition upon review of my flow in the queue.

I've gone so far as to arbitarily grab "NOW()" and attempt to toNumber() it but with the same result as trying to modify the above attributes.  incidentally NOW() returns a format like: Mon Jul 24 15:39:21 UTC 2023 - its different that my UTC formatted items, but it also doesn't want to toNumber() - returning again an empty value


Screenshot 2023-07-24 133844.png
Shows me attempting to convert the attributes:
image
and this shows the flow in the queue after hitting that updateattrs.  The datetimestamps are all there, but the toNumber() fails for both format styles...  timenow was set earlier with a simple ${now()}, and none of them are formatted.  when attempting to format them, actually, i get other errors.


I'm struggling to work with dates in nifi and hoping someone has some guidance for me.  

NOTE: yes, my nifi is reskinned - its easier on the eyes.

1 ACCEPTED SOLUTION

avatar
Super Guru

Hi @maglinvinn ,

 

To get the expected result, you need to convert the string type datetime to datetime type before calling the toNumber() function. The conversion can be done using the toDate() function  (see: https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#todate ), For example the arrivaltimeinmillis will have the following value:

 

${timearrival:toDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"):toNumber()}

 

If you find this is helpful, please accept solution.

Thanks

View solution in original post

2 REPLIES 2

avatar
Super Guru

Hi @maglinvinn ,

 

To get the expected result, you need to convert the string type datetime to datetime type before calling the toNumber() function. The conversion can be done using the toDate() function  (see: https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#todate ), For example the arrivaltimeinmillis will have the following value:

 

${timearrival:toDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"):toNumber()}

 

If you find this is helpful, please accept solution.

Thanks

avatar
Frequent Visitor

thank you so much! i'm moving forward again!