在apache-nifi中使用groovy

时间:2017-08-10 12:45:34

标签: groovy apache-nifi

我已经生成了带有属性日期的流文件,然后我想对我的日期进行一些更改:

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)

但executioncript posecor给了我异常:无法在null对象上调用getAtribute,我该怎么办?

2 个答案:

答案 0 :(得分:0)

我想这是解决方案:

 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)

答案 1 :(得分:0)

如果您的date属性值为2012/02/02 00:00:00.000'Z'

解析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_6201328133600000

PS:我仍然不明白你期望的价值是什么6214887820800000