AttributeError: PyStreamCallback对象没有属性长度



我正在尝试读取流文件内容,这是JSON文件,计数这个JSON元素数组的长度,然后将其写入一个新的属性。

但不知何故,我总是得到上述错误。我做错了什么?

事先谢谢你!

这是我的脚本

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

class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
jsn = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
array = json.loads(jsn) # type: dict
i = 0
while i <= 1:
root_key = list(array.keys())[0]
array = array[root_key]
i += 1
self.length = str(len(array))
def get_length_of_array(self):
return self.length
# end class
flowfile = session.get()
if(flowfile != None):
flowfile = session.write(flowfile, PyStreamCallback())
flowfile = session.putAttribute(flowfile, "length", PyStreamCallback().get_length_of_array())
session.transfer(flowFile, REL_SUCCESS)

您正在创建PyStreamCallback()对象2次。对于第二个实例,length属性还没有定义。

应该是这样的:

# end class
flowfile = session.get()
if(flowfile != None):
callback = PyStreamCallback()
flowfile = session.write(flowfile, callback)
flowfile = session.putAttribute(flowfile, "length", callback.get_length_of_array())
session.transfer(flowFile, REL_SUCCESS)

最新更新