读取不适用于groovy

时间:2018-05-30 01:46:49

标签: groovy apache-nifi

我系统中的nifi.properties文件中定义了一个变量,我尝试使用ExecuteScriptGroovy处理器内阅读。以下是我尝试过:

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)
}

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()

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

另外,根据我上面提到的错误堆栈,它并没有提到究竟缺少什么属性。有没有人知道如何自定义代码,因为它会错误地生成哪个属性缺失或类似的错误?

1 个答案:

答案 0 :(得分:4)

快速回答是脚本正文或ExecuteScript属性提供的脚本文件中不支持NiFi表达式语言,但您仍然可以完成所需。

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

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)
}

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

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)
}

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