2017-09-14 21 views
0

我用ExecuteScript處理器和Python語言編寫腳本。如何將FlowFile的屬性放入其JSON內容中?

我想將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() 
+0

我真的不知道蟒蛇,但回調實際上有flowFile訪問其回調外獲得? –

+0

@BryanBende:我怎樣才能訪問它?這是個問題。我只想將兩個屬性的內容複製到FlowFile的JSON內容中。應該有辦法做到這一點,因爲這是一項常見任務。也許有些處理器呢? – Dinosaurius

+0

如果要用新的JSON覆蓋內容,則可以使用ReplaceText並將替換值設置爲JSON字符串,如{「EventId」:$ {eventid}} –

回答

2

您可以使用EvaluateJsonPath並將目標設置爲流文件屬性,並將返回類型設置爲JSON。然後你就可以添加屬性,每個JSON路徑提取,如:

eventid = $.eventid 
+0

唯一的問題是'eventid'是一個包含'\'的Json字符串,例如'{「eventid」:「[{\」id1 \「:\」t234 \「,\」id2 \「:10},{...}]' – Dinosaurius

+0

包含'\'。這些符號? – Dinosaurius

+0

EvaluateJsonPath之後你可以使用UpdateAttribute來應用表達式語言函數,有一個替換函數 –

相關問題