Skip to content

Instantly share code, notes, and snippets.

@ijokarumawak
Created July 11, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijokarumawak/a4ef40b49b45cecf3c43b56493683725 to your computer and use it in GitHub Desktop.
Save ijokarumawak/a4ef40b49b45cecf3c43b56493683725 to your computer and use it in GitHub Desktop.
NiFi ExecuteScript example to lookup a configure path from an external xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration verbose="false" debugMode="false">
<dataFlows>
<dataFlow>
<properties>
<dept>salary</dept>
<version>1.0</version>
</properties>
<filePattern>salary_.*.gz</filePattern>
<to>
<path>d_${dept}/${version}/csv</path>
</to>
</dataFlow>
<dataFlow>
<properties>
<dept>pension</dept>
<version>2.2</version>
</properties>
<filePattern>pension_.*.gz</filePattern>
<to>
<path>d_${dept}/${version}/csv</path>
</to>
</dataFlow>
</dataFlows>
</configuration>
import java.util.regex.Pattern
import org.apache.nifi.attribute.expression.language.Query
import org.apache.nifi.flowfile.FlowFile
FlowFile flowFile = session.get()
if (!flowFile) return
def filename = flowFile.getAttribute('filename')
def configuration = new XmlSlurper().parse("/tmp/config.xml")
def dataFlow = configuration.dataFlows.dataFlow.find { Pattern.compile(it.filePattern.text()).matcher(filename).matches() }
if (dataFlow.isEmpty()) {
session.transfer(flowFile, REL_FAILURE)
return
}
def variables = [:]
variables['dept'] = dataFlow.properties.dept.text()
variables['version'] = dataFlow.properties.version.text()
def query = Query.prepare(dataFlow.to.path.text())
def path = query.evaluateExpressions(variables, null)
flowFile = session.putAttribute(flowFile, 'to.path', path)
session.transfer(flowFile, REL_SUCCESS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment