我从外部应用程序接收模板文本内容,并将如下所示的占位符加载到名为app_template的flowFile属性中:
Hello ${firstname} ${lastname},
thanks for contacting ${repname} and ${company}.模板中的占位符需要替换为NiFi属性,并在上游初始化,这些属性不是预先定义的。基本上,该模板可以包含任何属性名称。
是否有可能评估该模板并解析属性,以便NiFi使用相应的属性值替换${firstname}和${lastname},而不使用替换字符串函数?
像${app_template:evaluateAttributes()}这样简单的东西...
谢谢!
发布于 2019-02-23 04:47:26
您可以通过以下代码使用ExecuteGroovyScript处理器
import groovy.text.SimpleTemplateEngine
def ff=session.get()
if(!ff)return
//read template from flow file content
def template = ff.read().withReader("UTF-8"){r->
new SimpleTemplateEngine().createTemplate(r)
}
//get attributes and make a copy because for template it should be midifiable
def attr = new HashMap(ff.getAttributes())
//build template with attributes as binding variables
ff.write("UTF-8"){w->
template.make(attr).writeTo(w)
}
REL_SUCCESS << ff注意:
此模板支持groovy和<%= firstname %>表达式:groovy和groovy
您还可以使用类似于jsp的groovy代码注入:<% out.print( firstname.toUpperCase() ) %>
发布于 2018-02-17 12:51:49
您完全可以在Apache NiFi的表达式语言中计算其他属性。我相信您的问题是,您试图在定义其他属性的同时执行此操作,因此这不起作用(属性定义在单处理器配置中不是确定性的)。
但是,通过使用UpdateAttribute处理器,您可以根据自己的喜好使用其他属性的内容填充属性。我创建了a template which does this来演示。


--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
Value: 'Fri Feb 16 20:48:52 PST 2018'
Key: 'lineageStartDate'
Value: 'Fri Feb 16 20:48:52 PST 2018'
Key: 'fileSize'
Value: '63'
FlowFile Attribute Map Content
Key: 'appendFullName'
Value: 'Andy LoPresto'
Key: 'filename'
Value: '422457211196462'
Key: 'firstName'
Value: 'Andy'
Key: 'fullName'
Value: ' '
Key: 'joinFullName'
Value: 'Andy LoPresto'
Key: 'lastName'
Value: 'LoPresto'
Key: 'path'
Value: './'
Key: 'substitutionFullName'
Value: 'Andy LoPresto'
Key: 'uuid'
Value: '2a8ba776-e059-46b2-bc5e-ccafd509356d'
--------------------------------------------------
This is a message generated at 2018/02/16 20:48:52.362 -0800.发布于 2018-02-17 10:32:24
我刚刚找到了另一个SO的答案,看起来唯一的方法就是使用脚本处理器In Apache NiFi, can I evaluate expression language without an attribute?
https://stackoverflow.com/questions/48837255
复制相似问题