Created
July 11, 2017 12:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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