在 apache-nifi 中使用 groovy
Using groovy in 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)
但是 executescript posecor 让我 exception:Cannot 在空对象上调用 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)
如果您的 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
处理器_
只需使用以下 nifi expression:
定义名为 date
的新 属性
12321312_${date:toDate("yyyy/MM/dd HH:mm:ss.SS"):toNumber():plus(6200000000000000)}
两种变体都给出了结果:
12321312_6201328133600000
PS:我还是不明白你期望的值是多少6214887820800000
我已经生成了带有日期属性的流文件,然后我想对我的日期进行一些更改:
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 让我 exception:Cannot 在空对象上调用 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)
如果您的 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
处理器_
只需使用以下 nifi expression:
定义名为date
的新 属性
12321312_${date:toDate("yyyy/MM/dd HH:mm:ss.SS"):toNumber():plus(6200000000000000)}
两种变体都给出了结果:
12321312_6201328133600000
PS:我还是不明白你期望的值是多少6214887820800000