Support Questions

Find answers, ask questions, and share your expertise

How can I get Nifi expression language to work with Properties added to ExecuteScript?

avatar
Super Collaborator

In an ExecuteScript processor, I added a property called testProperty and set the value to ${filename}. But when I reference it in my groovy script, I get "${filename}" rather than the value of the 'filename' in the incoming flowfile. ExecuteSripts says that properties support expression language, but I'm not seeing it.

My test script is simple and I watch the logs.

log.info(" -------------------------" + testProperty)

It prints this but filename is a pre-existing attribute:

-------------------------${filename}

Thanks,

Jim

1 ACCEPTED SOLUTION

avatar
Master Guru

Dynamic properties (like your "testProperty") are passed in as variables, and they are PropertyValue objects. PropertyValue has a method called evaluateAttributeExpressions() and if you want to to resolve attributes from a FlowFile, you can pass in a reference to that flow file. Then you call getValue() (or just ".value" in Groovy) and the property will have been evaluated correctly.

Since you're using the "filename" attribute, I assume you will be getting that from an incoming flow file. So you will need to get the flow file from the session, pass it into evaluateAttributeExpressions(), then don't forget to transfer or remove the flow file. Here is an example in Groovy:

flowFile = session.get()
if(!flowFile) return 
log.info("-----------------------" + testProperty.evaluateAttributeExpressions(flowFile).value)
session.transfer(flowFile, REL_SUCCESS)

View solution in original post

2 REPLIES 2

avatar
Master Guru

Dynamic properties (like your "testProperty") are passed in as variables, and they are PropertyValue objects. PropertyValue has a method called evaluateAttributeExpressions() and if you want to to resolve attributes from a FlowFile, you can pass in a reference to that flow file. Then you call getValue() (or just ".value" in Groovy) and the property will have been evaluated correctly.

Since you're using the "filename" attribute, I assume you will be getting that from an incoming flow file. So you will need to get the flow file from the session, pass it into evaluateAttributeExpressions(), then don't forget to transfer or remove the flow file. Here is an example in Groovy:

flowFile = session.get()
if(!flowFile) return 
log.info("-----------------------" + testProperty.evaluateAttributeExpressions(flowFile).value)
session.transfer(flowFile, REL_SUCCESS)

avatar
Super Collaborator

Awesome! Thanks for the full sample.

By digging through source code I figured out that I could use the property object with evaluateExpressions(flowFile), but I did not realize that testProperty is a property already. duh.

This is what I had tried but it printed "null", so assumed I was going down the wrong path.

flowFile = session.get()
if(!flowFile) return
def retVal = context.getProperty('testProperty').evaluateExpressions(flowFile)
log.info("-----------------------"+retVal?.value)
session.transfer(flowFile, REL_SUCCESS)