groovy脚本在nifi executescript处理器中不工作

groovy脚本在nifi executescript处理器中不工作,groovy,apache-nifi,Groovy,Apache Nifi,我正试图通过executescript处理器执行一些东西;里面有一个groovy代码。在代码中,我试图创建一个scala脚本,该脚本将在另一个处理器的spark上执行 // Get flow file def flowFile = session.get() if (!flowFile) return // Create output directory def userInputDir = flowFile.getAttribute("user.input.path") def finalFo

我正试图通过executescript处理器执行一些东西;里面有一个groovy代码。在代码中,我试图创建一个scala脚本,该脚本将在另一个处理器的spark上执行

// Get flow file
def flowFile = session.get()
if (!flowFile) return
// Create output directory
def userInputDir = flowFile.getAttribute("user.input.path")
def finalFolder = new File(userInputDir + "/" + innerDir)

try
{
if (!finalFolder.exists()) finalFolder.mkdirs()
// Write script
file = "spark.sqlContext.setConf(\"hive.exec.dynamic.partition\", \"true\")\n"
file = file + "spark.sqlContext.setConf(\"hive.exec.dynamic.partition.mode\", \"nonstrict\")\n"
file = file + "import org.apache.spark.sql._"
file = file + "\n"
file = file + "import java.io._"
file = file + "\n"
} . . . 其余步骤是向
脚本
变量添加一些其他特定于spark的命令。脚本很大,因此跳过完整的代码粘贴。 最后,以接球结束

// Output file path
flowFile = session.putAttribute(flowFile, "generatedScript", scalaFile.getCanonicalPath())
session.transfer(flowFile, REL_SUCCESS)
}
catch(Exception e)
{
 log.info("File: {}\n", finalFolder.file)
 session.transfer(flowFile, REL_FAILURE)
}
处理器甚至还没有开始启动groovy脚本执行,它失败并出现以下错误:

groovy.lang.MissingPropertyException: No such property: script for calss: javal.io.File
语句“not even beging to start”表示前一个队列不是空的,处理器抛出错误。我猜这是一个语法问题,但我在脚本中没有发现任何相关的语法问题。我还尝试在本地机器的GroovyShell中运行该脚本,但也出现了相同的错误,但没有语法问题

通过谷歌搜索错误,我得到了在脚本中包含导入的建议,但即使在包含相关导入之后,错误也是一样的


有什么线索吗?

您指的是一个未在任何地方定义的变量“innerDir”。是否希望用户向名为innerDir的ExecuteScript添加用户定义的属性?如果是这样,脚本中的innerDir变量是一个PropertyValue对象,因此需要对其调用getValue()以获取属性的实际值:

innerDir.value
您还提到了scalaFile.getCanonicalPath(),但是上面没有定义scalaFile,getCanonicalPath()不会提供脚本的内容,这就是您的意思吗

我修改了上面的部分脚本,假设innerDir是一个用户定义的属性,您将file变量的内容写入scalaFile指向的文件;此外,我还通过使用herdoc而不是附加到file变量使其更具Groovy性:

// Get flow file
def flowFile = session.get()
if (!flowFile) return
// Create output directory
def userInputDir = flowFile.getAttribute("user.input.path")
def finalFolder = new File(userInputDir + "/" + innerDir?.value ?: '')

try
{
  if (!finalFolder.exists()) finalFolder.mkdirs()
  // Write script
  file = 
"""
  spark.sqlContext.setConf("hive.exec.dynamic.partition", "true")
  spark.sqlContext.setConf("hive.exec.dynamic.partition.mode", "nonstrict")
  import org.apache.spark.sql._
  import java.io._
"""
  scalaFile = new File(finalFolder, 'script.scala')
  scalaFile.withWriter {w -> w.write(file)}
  // Output file path
  flowFile = session.putAttribute(flowFile, "generatedScript", scalaFile.canonicalPath)
  session.transfer(flowFile, REL_SUCCESS)
}
catch(Exception e) {
  log.info("File: {}\n", finalFolder.file)
  session.transfer(flowFile, REL_FAILURE)
}

您尝试访问“文件”对象上的“脚本”属性。一旦您没有提供完整的脚本和错误行-无法提供帮助(ihmo)。尝试使用ExecuteGroovy脚本处理器RemoveTryCatch,您将在日志文件中看到详细的stacktrace,其中包含脚本中的行号。。。更正了,谢谢你,马蒂布。有时候,小事情往往被忽略。