Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

how get next Week Sunday in NIFI

Solved Go to solution
Highlighted

how get next Week Sunday in NIFI

New Contributor

Example : Input : 10/12/2019 
I need to get subsequent Sunday of given input date

1 ACCEPTED SOLUTION

Accepted Solutions

Re: how get next Week Sunday in NIFI

Master Guru

@bha 

This can be done via a single long complex NiFi Expression Language (EL) statement or via a couple smaller EL statements within the "Advanced" Ui of the UpdateAttribute processor.

 

Before you can use NiFI EL to solve this query, you will need to get your date in to a FlowFile attribute.  Since i have no idea where the date 10/12/2019 originates from, I can't help much here.  If it is on the content, perhaps extractText processor can be used.

 

Lets assume we have a FlowFile with the following FlowFile attribute on it:

   

FlowFile attribute name: FlowFile Attribute value:
mydate 10/12/2019

 

We can pass this FlowFile through an UpdateAttribute processor where we create a new attribute with the date of the subsequent Sunday. (My EL assumes that if input date is already Sunday, it will calculate the next Sunday instead of just reporting that today is Sunday)

 

Solution1 (one long NiFi EL statement):

Screen Shot 2019-10-14 at 11.12.22 AM.png

The full EL is below (all one line):

 

${mydate:toDate('MM/dd/yyyy'):format('u'):lt('7'):ifElse("${literal('7'):minus(${mydate:toDate('MM/dd/yyyy'):format('u')}):multiply('86400000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}","${literal('604800000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}")}

 

NOTE: Be careful with copy paste as it may screw up the single and double quote marks in the above EL making the EL statement invalid.

 

What above does is calculate day of week (1 - 7)...

IF - Then if that number is less then 7:

      --- it subtracts it from 7 to determine how many day until next Sunday remain.
      --- Then multiples that number by number of millseconds in a day
      --- Then adds that number to the millisecond value of your input date.  
      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

ELSE - When number is not less then 7

      --- It adds 7 days in milliseconds to number to the millisecond value of your input date.

      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

 

So essential you have tow EL statements handled by an ifElse function. One covers days 1 -6 and the other covers day 7.

 

Solution 2 (Using "Advanced" UI of UpdateAttribute to break apart above long EL):

The Advanced Ui allows you to construct rules.  Only the rules where the "conditions" resolve to "true" will apply the actions. So we simply create two rules.  One for days 1-6 and the other for when day of week is 7.

Day less than 7:

Screen Shot 2019-10-14 at 11.31.25 AM.png

Day equal 7:

Screen Shot 2019-10-14 at 11.32.03 AM.png

Using the "Advanced"UI makes handling complex EL statements easier.

 

Hope this helps,
Matt

 

1 REPLY 1

Re: how get next Week Sunday in NIFI

Master Guru

@bha 

This can be done via a single long complex NiFi Expression Language (EL) statement or via a couple smaller EL statements within the "Advanced" Ui of the UpdateAttribute processor.

 

Before you can use NiFI EL to solve this query, you will need to get your date in to a FlowFile attribute.  Since i have no idea where the date 10/12/2019 originates from, I can't help much here.  If it is on the content, perhaps extractText processor can be used.

 

Lets assume we have a FlowFile with the following FlowFile attribute on it:

   

FlowFile attribute name: FlowFile Attribute value:
mydate 10/12/2019

 

We can pass this FlowFile through an UpdateAttribute processor where we create a new attribute with the date of the subsequent Sunday. (My EL assumes that if input date is already Sunday, it will calculate the next Sunday instead of just reporting that today is Sunday)

 

Solution1 (one long NiFi EL statement):

Screen Shot 2019-10-14 at 11.12.22 AM.png

The full EL is below (all one line):

 

${mydate:toDate('MM/dd/yyyy'):format('u'):lt('7'):ifElse("${literal('7'):minus(${mydate:toDate('MM/dd/yyyy'):format('u')}):multiply('86400000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}","${literal('604800000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}")}

 

NOTE: Be careful with copy paste as it may screw up the single and double quote marks in the above EL making the EL statement invalid.

 

What above does is calculate day of week (1 - 7)...

IF - Then if that number is less then 7:

      --- it subtracts it from 7 to determine how many day until next Sunday remain.
      --- Then multiples that number by number of millseconds in a day
      --- Then adds that number to the millisecond value of your input date.  
      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

ELSE - When number is not less then 7

      --- It adds 7 days in milliseconds to number to the millisecond value of your input date.

      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

 

So essential you have tow EL statements handled by an ifElse function. One covers days 1 -6 and the other covers day 7.

 

Solution 2 (Using "Advanced" UI of UpdateAttribute to break apart above long EL):

The Advanced Ui allows you to construct rules.  Only the rules where the "conditions" resolve to "true" will apply the actions. So we simply create two rules.  One for days 1-6 and the other for when day of week is 7.

Day less than 7:

Screen Shot 2019-10-14 at 11.31.25 AM.png

Day equal 7:

Screen Shot 2019-10-14 at 11.32.03 AM.png

Using the "Advanced"UI makes handling complex EL statements easier.

 

Hope this helps,
Matt

 

Don't have an account?
Coming from Hortonworks? Activate your account here