Support Questions

Find answers, ask questions, and share your expertise

NifiFlow variables update and posterior use into a ExecuteScript

avatar
Explorer

I have create a ProcessGroup to update NifiFlow variables that receives the new values via MQTT using a json structure. Previously I have defined the variables in the scope of the NifiFlow with default values.

To update the variables I use a EvaluateJsonPath configured as follow to process the json received.

Later, I try to use those variables into a ExecuterScript but the are not accessible or I don't known how to access to them. This is the script (javascript):

var flowFile = session.get()
if (flowFile != null) {
  var categories = flowFile.getAttribute('root.sensors.categories').split(',')
  var typesPerCategory = flowFile.getAttribute('root.sensors.types').split(';')
  var sensorType = flowFile.getAttribute('sensor.type')
  for (var i = categories.length; (--i) >= 0;) {
    var types = typesPerCategory[i].split(',')
    for (var j = types.lenght; (--j) >= 0;) {
      if (types[j].equals(sensorType)) {
        var topics = session.getAttribute(flowFile, 'root.sensors.topics').split(',')
        flowFile = session.putAttribute(flowFile, 'topic', topics[i])
        session.transfer(flowFile, REL_SUCCESS)
      }
    }
  }
  session.transfer(flowFile, REL_FAILURE)
}



nififlowvariables.pngevaluatejsonpath-settings.png
1 ACCEPTED SOLUTION

avatar
Master Guru

Variable registry is not the same thing as flow file attributes. What you are setting here are flow file attributes with the topic names, which will now be passed along with that specific flow file and accessible through expression language only when that particular flow file is involved. There is no programmatic access to the variable registry from a processor or ExecuteScript, it would have to be done via NiFi's REST API.

View solution in original post

2 REPLIES 2

avatar
Master Guru

Variable registry is not the same thing as flow file attributes. What you are setting here are flow file attributes with the topic names, which will now be passed along with that specific flow file and accessible through expression language only when that particular flow file is involved. There is no programmatic access to the variable registry from a processor or ExecuteScript, it would have to be done via NiFi's REST API.

avatar
Explorer

Thank you @Bryan Bende!

I thought variable registry could be modified on runtime to change workflow behavior. I think I'm not going to use the REST API to solve my design. It only implies penalization to the performance due to it is going to be done each time a new message from a sensor arrives. I could do the it obtaining those values from a DB but it has the same penalization.

I will do a more static approach, routing with attributes and changing the design if the categories and/or the types of the sensors change.