如何将 FlowFile 的属性放入其 JSON 内容中?

How to put attributes of FlowFile into its JSON content?

我使用 ExecuteScript 处理器和 Python 语言编写脚本。

我想将 FlowFile 的两个属性(eventidreason)作为 parameter:value 对传递到其 JSON 内容中。 eventid 的值为字符串,而 reason 的值为整数。我尝试使用 flowFile.getAttribute('eventid'),但失败了。

正确的方法是什么?

def process(self, inputStream, outputStream):
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
        obj = json.loads(text)
        dt = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')

        newObj = {
            "EventId": str(parse(flowFile.getAttribute('eventid'))),
            "EventType": self.getEventType(dt,obj),
            "EventReason": flowFile.getAttribute('reason')
        }
        outputStream.write(bytearray(json.dumps(newObj, indent=4).encode('utf-8')))

flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile, ModJSON())
session.transfer(flowFile, REL_SUCCESS)
session.commit()

您可以使用 EvaluateJsonPath,并将 Destination 设置为流文件属性并将 Return Type 设置为 JSON。然后你可以为每个 JSON 路径添加属性来提取,如:

eventid = $.eventid