在 executeScript NIFI 中使用 FlowFile 属性 python
Use FlowFile attributes in a executeScript NIFI python
我试图在我的 python 脚本中获取流文件的属性,我做了以下操作:
class TransformCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
try:
# Read input FlowFile content
input_text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
input_obj = json.loads(input_text)
但是如何在处理方法中访问我的流文件属性?
除非您执行某些操作(例如将对 FlowFile 的引用传递到 TransformCallback 构造函数中),否则它们不会立即在处理方法中可用。另一种选择是将读取和写入(因为您正在使用 IOUtils.toString() 一次读取整个内容)分成两个单独的调用,然后您可以在 process() 方法之外进行属性操作。
顺便说一句,如果您只需要将整个内容作为字符串读入,则不需要 StreamCallback 或 InputStreamCallback,您可以使用 session.read(flowFile) which returns InputStream(而不是执行提供的回调)。您可以对其调用 IOUtils.toString() (并且不要忘记之后关闭它),从而避免回调并允许使用当前的 FlowFile 引用(以及 getAttribute() 或 getAttributes 更容易地访问流文件属性() 方法).
我试图在我的 python 脚本中获取流文件的属性,我做了以下操作:
class TransformCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
try:
# Read input FlowFile content
input_text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
input_obj = json.loads(input_text)
但是如何在处理方法中访问我的流文件属性?
除非您执行某些操作(例如将对 FlowFile 的引用传递到 TransformCallback 构造函数中),否则它们不会立即在处理方法中可用。另一种选择是将读取和写入(因为您正在使用 IOUtils.toString() 一次读取整个内容)分成两个单独的调用,然后您可以在 process() 方法之外进行属性操作。
顺便说一句,如果您只需要将整个内容作为字符串读入,则不需要 StreamCallback 或 InputStreamCallback,您可以使用 session.read(flowFile) which returns InputStream(而不是执行提供的回调)。您可以对其调用 IOUtils.toString() (并且不要忘记之后关闭它),从而避免回调并允许使用当前的 FlowFile 引用(以及 getAttribute() 或 getAttributes 更容易地访问流文件属性() 方法).