[英]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.