首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算NiFi属性,包含其他属性

问如何计算NiFi属性,包含其他属性
EN

Stack Overflow用户
提问于 2018-02-17 10:17:22
回答 3查看 2.3K关注 0票数 1

我从外部应用程序接收模板文本内容,并将如下所示的占位符加载到名为app_template的flowFile属性中:

代码语言:javascript
复制
Hello ${firstname} ${lastname},

thanks for contacting ${repname} and ${company}.

模板中的占位符需要替换为NiFi属性,并在上游初始化,这些属性不是预先定义的。基本上,该模板可以包含任何属性名称。

是否有可能评估该模板并解析属性,以便NiFi使用相应的属性值替换${firstname}和${lastname},而不使用替换字符串函数?

像${app_template:evaluateAttributes()}这样简单的东西...

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-23 04:47:26

您可以通过以下代码使用ExecuteGroovyScript处理器

代码语言:javascript
复制
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() ) %>

票数 2
EN

Stack Overflow用户

发布于 2018-02-17 12:51:49

您完全可以在Apache NiFi的表达式语言中计算其他属性。我相信您的问题是,您试图在定义其他属性的同时执行此操作,因此这不起作用(属性定义在单处理器配置中不是确定性的)。

但是,通过使用UpdateAttribute处理器,您可以根据自己的喜好使用其他属性的内容填充属性。我创建了a template which does this来演示。

​

​

​

​

代码语言:javascript
复制
--------------------------------------------------
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.
票数 4
EN

Stack Overflow用户

发布于 2018-02-17 10:32:24

我刚刚找到了另一个SO的答案,看起来唯一的方法就是使用脚本处理器In Apache NiFi, can I evaluate expression language without an attribute?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48837255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档