【问题标题】:Load file content into nifi attribute using executescript使用 executescript 将文件内容加载到 nifi 属性中
【发布时间】:2019-07-30 14:45:23
【问题描述】:

我正在尝试将文件的内容加载到属性中并以“Bearer”开头。这是我走了多远:

def flowFile = session.get();
if (flowFile != null) {

    def token = flowFile.getAttribute("token")
    def message = "Bearer " + token
    flowFile = session.putAttribute(flowFile, "message", message)
    session.transfer(flowFile, REL_SUCCESS)
}

但是,我一直在尝试加载文件内容而不是使用 .getattribute。谁能帮帮我吗?

编辑:确认一下,这是加载 txt 文件的内容。

【问题讨论】:

    标签: groovy apache-nifi


    【解决方案1】:

    您可以使用ExecuteGroovyScript 和以下代码来读取流文件内容并将其放入属性中:

    def ff = session.get()
    if(!ff)return
    
    ff.message = "Bearer " + ff.read().getText("UTF-8")
    REL_SUCCESS << ff
    

    要读取普通文件只需替换ff.read().getText("UTF-8")

    new File("path/to/file.txt").getText("UTF-8")


    注意:注意将大值存储到属性中。

    【讨论】:

    • 嗨,Daggett,非常感谢你。请问,我打算在哪里定义文件的位置,是这样的吗? ff.read(path/to.file.txt)...
    • 啊。你的意思是文件(不是流文件)
    • 对不起,我不清楚,我的意思是从文本文件中,将整个内容读入流文件属性。文件的内容总是很小。
    • 修改答案
    • 应该可以。但请确保您使用的是 ExecuteGroovyScript(不是 ExecuteScript)
    【解决方案2】:

    您需要使用流文件和 InputStreamCallback 调用 session.read。

    这里有一个例子:

    https://funnifi.blogspot.com/2016/08/executing-remote-commands-in-nifi-with.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2020-05-30
      • 2023-03-09
      相关资源
      最近更新 更多