Skip to main content
 首页 » 编程设计

python之在 executeScript NIFI python 中使用 FlowFile 属性

2024年10月17日8sky-heaven

我试图在我的 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) 

但是我如何在 process 方法中访问我的流文件属性?

请您参考如下方法:

它们不会立即在 process 方法中可用,除非您执行某些操作,例如将对 FlowFile 的引用传递到您的 TransformCallback 构造函数中。另一种选择是将读取和写入(因为您正在使用 IOUtils.toString() 一次读取整个内容)拆分为两个单独的调用,然后您可以在 process() 方法之外进行属性操作。

顺便说一句,如果您只需要将整个内容作为字符串读入,则不需要 StreamCallback 或 InputStreamCallback,您可以使用返回 InputStream 的 session.read(flowFile)(而不是执行提供的打回来)。您可以在上面调用 IOUtils.toString() (并且不要忘记之后关闭它),从而避免回调并允许使用当前的 FlowFile 引用(以及 getAttribute() 或 getAttributes()方法)。