我已经生成了带有date属性的flowfile,然后我想对我的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)但executescript posecor给我异常:无法在空对象上调用getAtribute,我该怎么办?
发布于 2017-08-11 16:16:40
如果您有值为:2012/02/02 00:00:00.000'Z'的date属性
解析date并将其转换为毫秒的groovy代码可能如下所示:
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)以上所有操作您都可以使用UpdateAttribute processor_完成
只需使用以下nifi expression定义名为date的新属性
12321312_${date:toDate("yyyy/MM/dd HH:mm:ss.SS"):toNumber():plus(6200000000000000)}这两种变体都给出了结果:
12321312_6201328133600000PS:我仍然不明白你期望的6214887820800000的值是多少
发布于 2017-08-11 15:35:14
我想这就是解决方案:
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)https://stackoverflow.com/questions/45614127
复制相似问题