【问题标题】:Using groovy in apache-nifi在 apache-nifi 中使用 groovy
【发布时间】:2017-08-10 12:45:33
【问题描述】:

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

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)

但是 executescriptposecor 给了我异常:Cannot invoke getAtribute on null object ,我该怎么办?

【问题讨论】:

  • 除了executescript之外还有其他nifi处理器可以用来做同样的工作
  • date 属性的格式是什么?这个神奇的数字是什么意思 - 621355968000000000(1900 万年)?在您的代码中,您必须将 if(!flowfile) return 放在 session.get() 的行之后
  • 从 1900 年 1 月到 1970 年 1 月有 621355968000000000 个纪元刻度。(我需要将日期转换为刻度)
  • 什么是日期格式?
  • ISO 标准.......

标签: groovy apache-nifi


【解决方案1】:

如果你有 date 属性值:2012/02/02 00:00:00.000'Z'

解析date 并将其转换为毫秒的常规代码可能如下所示:

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)

以上所有你都可以用UpdateAttributeprocessor_

只需使用以下nifi expression 定义名为date 的新属性:

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

两种变体都会给出结果:

12321312_6201328133600000

PS:我还是不明白你期望6214887820800000的值是多少

【讨论】:

  • 谢谢,在使用 updateprocessor 之后,我可以使用这种情况下的值 6201328133600000 作为在 invokehttp 中调用服务的参数,我已经在 ${date} 之前尝试过它(没有它'12321312')和它没用
  • 现在它的工作我错过了分号,谢谢它非常有帮助
【解决方案2】:

我想这是解决方案:

 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多