2016-03-11 68 views
0

我試圖在Apache NiFi中製作一個自定義處理器,它可以向流文件內容中的JSON對象添加屬性/字符串。目前它只在使用字符串時起作用,但當我使用NiFi的表達式語言時它不起作用,儘管我的代碼支持它。NiFi自定義處理器表達式語言

表達式語言100%正確,因爲它在另一個處理器中工作,我也嘗試了不同的屬性以確保它不是屬性。

屬性:

public static final PropertyDescriptor ADD_ATTRIBUTE = new PropertyDescriptor 
     .Builder().name("Add Attribute") 
     .description("Example Property") 
     .required(true) 
     .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) 
     .expressionLanguageSupported(true) 
     .build(); 

在我的代碼後來,當我想要得到的價值和投入的JSON對象我用:

jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions().getValue()); 

我也做了一個單元測試,它的工作原理當我將文本值分配給testrunner.setProperty時。但是我不知道如何將屬性分配給testrunner,或者我如何在測試中使用表達式語言。

在此先感謝您的任何建議或解決方案!

+0

什麼是你得到的返回值「 context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions()的getValue()「? –

+0

在評估表達式時,您是否需要輸入流文件中的任何內容?如果是這樣,一定要將flowfile傳遞給evaluateAttributeExpressions()調用。 –

+0

嗨,當我使用表達式語言時,返回值是一個空字符串。當我只使用常規字符串時,例如「測試」,則返回值爲「test」。我不需要任何東西,但也試圖看看它是否做了任何事情,但事實並非如此。 – Matthias

回答

4

我會把我的回答從Hortonworks Community Connection這裏也FWIW:

如果表達式是指在一個流文件的屬性,你將需要傳遞一個參考流程文件到evaluateAttributeExpressions:

FlowFile flowFile = session.get(); 
jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue()); 

如果屬性包含屬性名稱(而不是包含屬性名稱的表達式),你想從流文件中的值:

jsonObject.put("hostname", flowFile.getAttribute(context.getProperty(ADD_ATTRIBUTE).getValue())); 

如果屬性值本身包含表達式語言,你要評估它,看看下面的類:

org.apache.nifi.attribute.expression.language.Query 
1

關於測試...

假設你對傳入的計算表達式語言flowFile(evaluateAttributeExpressions(flowFile)),那麼你可以做到以下幾點:

runner.setProperty(ADD_ATTRIBUTE, "${my.attribute}");

然後創建具有my.attribute在它的屬性地圖:

final Map<String,String> attributes = new HashMap<>(); attributes.put("my.attribute", myAttribute);

然後排隊一些內容與屬性:

runner.enqueue(fileIn, attributes); runner.run();

從代碼庫的一個例子:

https://github.com/apache/nifi/blob/1e56de9521e4bc0752b419ffc7d62e096db1c389/nifi-nar-bundles/nifi-solr-bundle/nifi-solr-processors/src/test/java/org/apache/nifi/processors/solr/TestPutSolrContentStream.java#L243