How to do Dynamic property naming in NIfi?

270 Views Asked by At

I'm in a situation where I need to be able to set the name of a property using Expression Language. Is there any way to do that?

I try to create an attribute using an UpdateAttribute processor and put in ${property1} as the property name. Now, property1 is in reference to an existing attribute containing the value "property2". What I'd hope is that the flowfile has a new property added named "property2" but what I end up with is a new property literally named "${property1}".

Is this even possible? How to do this using JOlt Spec?

3

There are 3 best solutions below

0
On

No, you cannot use Expression Language to dynamically set the property name of an UpdateAttribute processor directly.

Here's a workaround, use:

  1. AttributesToJSON - to convert the attributes to a JSON object.
  2. JoltTransformJSON to manipulate the JSON object using Jolt Specification.
  3. JSONToAttributes to convert the JSON object back to attributes.
2
On

You can gather the literal property2 as prop : property2 key-value pair within a JSON using a JoltTransformJSON processor along with a such specification

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "prop":"${property1}" // yields "prop" : "property2"
    }
  }
]

possibly added an UpdateAttribute processor stated previously in the flow with

property name - value : property1 - property2

2
On

you can use ExecuteGroovyScript

def ff=session.get()
if(!ff)return

ff[ff.property1] = ff.property2

REL_SUCCESS<<ff

or ExecuteScript with Javascript language

var ff = session.get();
if (ff != null) {
    var k = ff.getAttribute( 'property1' );
    var v = ff.getAttribute( 'property2' );
    ff = session.putAttribute( ff, k, v );
    session.transfer( ff, REL_SUCCESS );
}