在NiFi Python脚本中解析JSON

时间:2019-02-19 11:37:43

标签: python jython apache-nifi

我基本上接收到流文件,并且我必须获取密钥的值。

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


class FlowFileParser(StreamCallback):
def __init__(self):
    pass
def process(self, inputStream, outputStream):
    text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    obj = json.loads(text)
    newObj = obj['priority']
    outputStream.write(bytearray(newObj.encode('utf-8')))



flowFile = session.get()
if flowFile != None:
    #flowFile = session.putAttribute(flowFile, "priority", "5")
    priority = FlowFileParser()
    flowFile = session.putAttribute(flowFile, "filename", priority)
    session.transfer(flowFile, REL_SUCCESS)

但是我收到下面的异常“ putAttribute():第3个arg不能被强制转换为第23行的String”

输入流文件可以为

{
"name": "martin",
"priority":"5"
}

我只希望文件名是5

1 个答案:

答案 0 :(得分:2)

您正在尝试将对象传递给字符串参数:

priority = FlowFileParser()
flowFile = session.putAttribute(flowFile, "filename", priority)

优先级是对FlowFileParser()的对象引用,而putAttribute的第三个参数必须是字符串。