Nifi在ExecuteScript中出错

时间:2017-09-08 11:35:51

标签: java python json apache-nifi

我一直在尝试从nifi的ExecuteScript Processor中提取数据,并将其作为属性附加到流文件中。我曾尝试过很多来源,特别是在Matt Burgess的funnifi博客上。

以下是我的代码

import json
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

originalFlowFile = session.get()
text = IOUtils.toString(originalFlowFile)
log.info(text)

if(originalFlowFile != None):
    event = json.loads(text)
    if (event['true'] == 'Y'):
        flowfile = session.putAttribute(flowfile, "true", "Y")
    elif (event['src'] == 'ONE' ):
        allAttributes = { "true": "N", "src": "ONE" }
        flowfile = session.putAllAttributes(flowfile, allAttributes)
    elif (event['src'] == 'TWO' ):
        allAttributes = { "true": "N", "src": "TWO" }
        flowfile = session.putAllAttributes(flowfile, allAttributes)
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()

这是在python中。流文件数据是JSON。不过,我无法解析它。

INFO部分是

的输出
text = IOUtils.toString(originalFlowFile)

The error I am getting

非常感谢任何帮助。

P.S。我不熟悉python

测试数据

{
  "true":"N",
  "src":"ONE",
  "var1":"value1",
  "var2":"value2"
}

更新

我的更新代码仍然不起作用:

import json
import java.io
from org.apache.commons.io import IOUtils

originalFlowFile = session.get()

if(originalFlowFile != None):
    inputStream = session.read(originalFlowFile)
    text = IOUtils.toString(inputStream)
    log.info(text)
    event = json.loads(text)
     if (event['true'] == 'Y'):
        flowfile = session.putAttribute(flowfile, "true", "Y")
    elif (event['src'] == 'ONE' ):
        allAttributes = { "true": "N", "src": "ONE" }
        flowfile = session.putAllAttributes(flowfile, allAttributes)
    elif (event['src'] == 'TWO' ):
        allAttributes = { "true": "N", "src": "TWO" }
        flowfile = session.putAllAttributes(flowfile, allAttributes)
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()

1 个答案:

答案 0 :(得分:3)

在流文件本身上调用IOUtils.toString()可能不会起作用,因为流文件不是InputStream,或Reader,或者可以自己读取的东西。我真的很惊讶这条线不会产生异常。

有两种方法可以获取流文件的内容......

第一种方法是从会话中获取流文件的InputStream:

originalFlowFile = session.get();
inputStream = session.read(originalFlowFile);
text = IOUtils.toString(inputStream);

第二种方法是使用InputStreamCallback:

flowFile = session.read(flowFile, {inputStream ->
   // read the inputStream
} as InputStreamCallback);