在apachenifi中使用groovy

在apachenifi中使用groovy,groovy,apache-nifi,Groovy,Apache Nifi,我已生成属性为date的flowfile,然后我想对我的日期进行一些更改: 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

我已生成属性为date的flowfile,然后我想对我的日期进行一些更改:

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给了我一个例外:无法在null对象上调用getAtribute,我该怎么办?

我想这就是解决方案:

 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)

如果您具有值为:
2012/02/02 00:00:00.000'Z'

解析日期并将其转换为毫秒的groovy代码如下: 所有这些都可以通过
UpdateAttribute
处理器完成_ 只需使用以下内容定义名为
date
的新属性:

两种变体都给出了结果:
PS:我仍然不明白您期望的值是多少
6214887820800000

除了executescript之外,是否有任何nifi处理器可以用于执行相同的任务
日期属性中的格式是什么?这个神奇的数字是什么意思?
62135596800000000
(1900万年)?在您的代码中,您必须将
if(!flowfile)return
放在带有
session.get()的行之后。
从1900年1月到1970年1月,共有62135596800000000个历元刻度。(我需要将日期转换为记号)日期格式是什么?ISO标准…….谢谢,在使用updateprocessor之后,我可以使用本例中的值62013213360000作为在invokehttp中调用服务的参数吗,我在${date}(没有它“12321312”)时尝试过它,但它不起作用现在它起作用了,我使用了分号,谢谢,这很有帮助
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)
12321312_${date:toDate("yyyy/MM/dd HH:mm:ss.SS"):toNumber():plus(6200000000000000)}
12321312_6201328133600000