我用ExecuteScript
處理器和Python語言編寫腳本。如何將FlowFile的屬性放入其JSON內容中?
我想將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()
我真的不知道蟒蛇,但回調實際上有flowFile訪問其回調外獲得? –
@BryanBende:我怎樣才能訪問它?這是個問題。我只想將兩個屬性的內容複製到FlowFile的JSON內容中。應該有辦法做到這一點,因爲這是一項常見任務。也許有些處理器呢? – Dinosaurius
如果要用新的JSON覆蓋內容,則可以使用ReplaceText並將替換值設置爲JSON字符串,如{「EventId」:$ {eventid}} –