读取 NiFi 系统属性不适用于 groovy

kno*_*one 0 groovy apache-nifi

nifi.properties在系统的文件中定义了一个变量,我试图ExecuteScript使用Groovy. 以下是我尝试过的:

def flowFile = session.get()
if (!flowFile) return

try
{
def newFile = new File(${config.password.file}).getText()

flowFile = session.putAttribute(flowFile, "passwordFile", newFile.toString())
session.transfer(flowFile, REL_SUCCESS)  
}
catch(Exception e)
{
flowFile = session.putAttribute(flowFile, "errorStack",e.toString())
session.transfer(flowFile, REL_FAILURE)
}
Run Code Online (Sandbox Code Playgroud)

的值config.password.file是包含我需要使用的密码的文件的绝对路径。

不确定,但这种方法行不通。这是我得到的错误堆栈:

groovy.lang.MissingPropertyException: No such property: config for class: Script90

我尝试使用常规功能,通过下面的代码从本地计算机上的文件中读取密码,效果很好。

def filex = "C:\\Users\\myUser\\Desktop\\passwordFile.pass"
String passFile = new File(filex).getText()
Run Code Online (Sandbox Code Playgroud)

知道我错过了什么/做错了什么吗?

另外,根据我上面提到的错误堆栈,它没有具体提到缺少什么属性。有谁知道如何自定义代码,因为它会生成错误,确切地说缺少哪个属性或类似的东西?

mat*_*tyb 5

简单的回答是,脚本主体或 ExecuteScript 属性提供的脚本文件中不支持 NiFi 表达式语言,但您仍然可以完成您想要的操作。

是如何config.password.file定义的?它是 ExecuteScript 处理器上的用户定义属性吗?如果是这样,问题就在于访问具有 Groovy 特定字符(例如句点)的变量。您必须使用脚本的绑定,而不是通过名称引用它们,如下所示:

def flowFile = session.get()
if (!flowFile) return

try {
  def newFile = new File(binding.getVariable('config.password.file').value).text
  flowFile = session.putAttribute(flowFile, "passwordFile", newFile.toString())
  session.transfer(flowFile, REL_SUCCESS)  
}
catch(e) {
  flowFile = session.putAttribute(flowFile, "errorStack",e.toString())
  session.transfer(flowFile, REL_FAILURE)
}
Run Code Online (Sandbox Code Playgroud)

如果它不是属性而是流文件属性,请尝试以下操作:

def flowFile = session.get()
if (!flowFile) return

try {
  def newFile = new File(flowFile.getAttribute('config.password.file')).text
  flowFile = session.putAttribute(flowFile, "passwordFile", newFile.toString())
  session.transfer(flowFile, REL_SUCCESS)  
}
catch(e) {
  flowFile = session.putAttribute(flowFile, "errorStack",e.toString())
  session.transfer(flowFile, REL_FAILURE)
}
Run Code Online (Sandbox Code Playgroud)

这两个脚本的区别仅在于 newFile 行,前者从与脚本关联的 Binding 获取属性作为 PropertyValue 对象,后者获取流文件属性的值。