簡體   English   中英

如何將FlowFile的屬性放入其JSON內容?

[英]How to put attributes of FlowFile into its JSON content?

我使用ExecuteScript處理器和Python語言編寫腳本。

我想將FlowFile的兩個屬性( eventidreason )作為參數:值對傳遞到其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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM