Support Questions
Find answers, ask questions, and share your expertise

Accessing processor properties in Execute Script

Super Collaborator

Hi All,

I am using a JavaScript Execute Script Processor. However, I am missing a document that details on how to access the dynamic properties set on the processor within the script body.

Any help appreciated.

Thanks

-ak-

1 ACCEPTED SOLUTION

Arun,

Edit:

Sorry, I got this confused with another question I was answering at the same time. To access dynamic properties in the script body, just reference them as a variable name.

From Matt Burgess' blog:

Dynamic Properties: Any dynamic properties defined in ExecuteScript are passed to the script engine as variables set to the string value of the property values. This means you must be aware of the variable naming properties for the chosen script engine. For example, Groovy does not allow periods (.) in variable names, so don't use something like "my.property" as a dynamic property name.

Here is the example output from a LogAttribute processor following an ExecuteScript processor where I update the flowfile by adding an attribute with the content of a dynamic property on the ExecuteScript processor.

Processor config:

12716-screen-shot-2017-02-20-at-52405-pm.png

Groovy script (note casting dynamic to String because by default it is of type org.apache.nifi.attribute.expression.language.StandardPropertyValue😞

def flowfile = session.get()
if (!flowfile) return
log.info("Dynamic property value: ${dynamic as String}")
flowfile = session.putAttribute(flowfile, "dynamic", dynamic as String)
session.transfer(flowfile, REL_SUCCESS)
2017-02-20 17:22:04,072 INFO [Timer-Driven Process Thread-3] o.a.n.processors.standard.LogAttribute LogAttribute[id=5e4107e1-015a-1000-5efa-735bf688a3d0] logging for flow file StandardFlowFileRecord[uuid=4edb12f2-a020-4c3e-b42b-0796b8e91031,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1487639391782-1, container=default, section=1], offset=725, length=29],offset=0,name=420715910236852,size=29]
--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
 Value: 'Mon Feb 20 17:22:04 PST 2017'
Key: 'lineageStartDate'
 Value: 'Mon Feb 20 17:22:04 PST 2017'
Key: 'fileSize'
 Value: '29'
FlowFile Attribute Map Content
Key: 'dynamic'
 Value: 'This is the value of a dynamic processor property set by Andy.'
Key: 'filename'
 Value: '420715910236852'
Key: 'path'
 Value: './'
Key: 'uuid'
 Value: '4edb12f2-a020-4c3e-b42b-0796b8e91031'
--------------------------------------------------
This is the flowfile content.

Original answer (referencing attributes):

You extract them from the flowfile object and write them to the flowfile via the session object. flowfile.getAttribute(attribute_name) returns the attribute_value, and session.putAttribute(flowfile, attribute_name, attribute_value) returns the new flowfile instance.

Example:

 flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')

View solution in original post

3 REPLIES 3

Arun,

Edit:

Sorry, I got this confused with another question I was answering at the same time. To access dynamic properties in the script body, just reference them as a variable name.

From Matt Burgess' blog:

Dynamic Properties: Any dynamic properties defined in ExecuteScript are passed to the script engine as variables set to the string value of the property values. This means you must be aware of the variable naming properties for the chosen script engine. For example, Groovy does not allow periods (.) in variable names, so don't use something like "my.property" as a dynamic property name.

Here is the example output from a LogAttribute processor following an ExecuteScript processor where I update the flowfile by adding an attribute with the content of a dynamic property on the ExecuteScript processor.

Processor config:

12716-screen-shot-2017-02-20-at-52405-pm.png

Groovy script (note casting dynamic to String because by default it is of type org.apache.nifi.attribute.expression.language.StandardPropertyValue😞

def flowfile = session.get()
if (!flowfile) return
log.info("Dynamic property value: ${dynamic as String}")
flowfile = session.putAttribute(flowfile, "dynamic", dynamic as String)
session.transfer(flowfile, REL_SUCCESS)
2017-02-20 17:22:04,072 INFO [Timer-Driven Process Thread-3] o.a.n.processors.standard.LogAttribute LogAttribute[id=5e4107e1-015a-1000-5efa-735bf688a3d0] logging for flow file StandardFlowFileRecord[uuid=4edb12f2-a020-4c3e-b42b-0796b8e91031,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1487639391782-1, container=default, section=1], offset=725, length=29],offset=0,name=420715910236852,size=29]
--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
 Value: 'Mon Feb 20 17:22:04 PST 2017'
Key: 'lineageStartDate'
 Value: 'Mon Feb 20 17:22:04 PST 2017'
Key: 'fileSize'
 Value: '29'
FlowFile Attribute Map Content
Key: 'dynamic'
 Value: 'This is the value of a dynamic processor property set by Andy.'
Key: 'filename'
 Value: '420715910236852'
Key: 'path'
 Value: './'
Key: 'uuid'
 Value: '4edb12f2-a020-4c3e-b42b-0796b8e91031'
--------------------------------------------------
This is the flowfile content.

Original answer (referencing attributes):

You extract them from the flowfile object and write them to the flowfile via the session object. flowfile.getAttribute(attribute_name) returns the attribute_value, and session.putAttribute(flowfile, attribute_name, attribute_value) returns the new flowfile instance.

Example:

 flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')

Super Collaborator

Hi @Andy LoPresto : I was asking about the dynamic properties set for the processor. Are they treated the same as attributes? I haven't tried accessing them by the getAttribute, will give it a try.

Super Collaborator

Thanks @Andy LoPresto. This helps.

; ;