3
votes

I have generated flowfile with attribute date and then I want to make some changes on my date:

import java.nio.charset.StandardCharsets 
import org.apache.commons.io.IOUtils
import org.apache.nifi.processor.io.StreamCallback

def flowfile = session.get()
def date=flowfile.getAttribute('date')
 def yourDate= new GregorianCalendar(date)
 def newdate= yourDate.getTimeInMillis()+621355968000000000
if(!flowfile) return
flowfile = session.putAttribute(flowfile, '12321312'+'_'+newdate)
session.transfer(flowfile, REL_SUCCESS)

but executescript posecor gives me exception:Cannot invoke getAtribute on null object , what should i do?

2
is there any nifi processor except executescript i can use to do same job - user7516956
what is the format in the date attribute? what means this magic number - 621355968000000000 (19M years)? in your code you have to put if(!flowfile) return just after the line with session.get() - daggett
There are 621355968000000000 epoch ticks Ist Jan 1900 to Ist Jan 1970. (i need to convert date to Ticks) - user7516956
and what is the date format? - daggett
ISO standart ....... - user7516956

2 Answers

2
votes

if you have the date attribute with value : 2012/02/02 00:00:00.000'Z'

the groovy code to parse the date and convert it to milliseconds could be like this:

def flowfile = session.get()
if(!flowfile) return
def date=flowfile.getAttribute('date')
//parse the string that contains date to java.util.Date
date = Date.parse('yyyy/MM/dd HH:mm:ss.SS', date)

//get milliseconds
def millis = date.getTime()
//add some magic number ?
millis+=6200000000000000
//set new attribute value
flowfile = session.putAttribute(flowfile, 'date', '12321312'+'_'+millis)
session.transfer(flowfile, REL_SUCCESS)

all above you can do with UpdateAttribute processor_

just define the new property named date with the following nifi expression:

12321312_${date:toDate("yyyy/MM/dd HH:mm:ss.SS"):toNumber():plus(6200000000000000)}

both variants gives the result:

12321312_6201328133600000

PS: I still don't understand what is the value that you expect 6214887820800000

0
votes

I guess this is solution:

 import java.nio.charset.StandardCharsets 
import org.apache.commons.io.IOUtils
import org.apache.nifi.processor.io.StreamCallback
import java.text.SimpleDateFormat
import java.util.GregorianCalendar

def flowfile = session.get()
if(!flowfile) return
def date=flowfile.getAttribute('fromDate')
 SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd");
  def d=new Date(format.parse(date).getTime());
  def newdate=new GregorianCalendar(d.getYear(),d.getMonth(),d.getDay() )
 def TICKS_AT_EPOCH = 621355968000000000;
 def TICKS_PER_MILLISECOND= 10000;
  def TickSolution=(newdate.getTimeInMillis() - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND

flowfile = session.putAttribute(flowfile, 'filename','Info'+'_'+date)
session.transfer(flowfile, REL_SUCCESS)