[英]Python ExecuteScript in NiFi: Transform flowfile attributes & content
[英]How to put attributes of FlowFile into its JSON content?
我使用ExecuteScript
处理器和Python语言编写脚本。
我想将FlowFile的两个属性( eventid
和reason
)作为参数:值对传递到其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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.