nifi-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Payne <marka...@hotmail.com>
Subject Re: How to reference attribute by variable name
Date Wed, 10 Jun 2020 14:47:16 GMT
Jim,

This isn’t a direct answer to your question. I don’t remember whether or not ExecuteScript
provides access directly to the ProcessContext or not - if it does, then that provides methods
for getting all properties and determining whether or not the property is “dynamic” (aka
user-defined).

But given the description of this script, why not simply use AttributesToJSON? It already
provides a property for supplying a comma-separated list of attributes and converts them into
a JSON payload.

Thanks
-Mark


On Jun 10, 2020, at 10:37 AM, James McMahon <jsmcmahon3@gmail.com<mailto:jsmcmahon3@gmail.com>>
wrote:

Hello. I currently have a python script in which I set values in a dictionary from flowfile
attributes, referencing them directly by name. I then create a json string, which I can send
to an AMQP broker as a message. Something like this:
result['absolute.path'] = flowFile.getAttribute('absolute.path')
result['filename'] = flowFile.getAttribute('filename')
result['myAttr'] = flowFile.getAttribute('myAttr')
# Serialize the object to JSON...
json_str = json.dumps(result)
# Replace flowfile payload with this string representation of my json...
outputStream.write(unicode(json_str).encode('utf-8'))

I need to generalize this. I want to create attribute properties in my ExecuteScript processor
from which I call this python, each property being a value I want to include in my json message.

Assuming I create an attribute property in the processor called draftPick, and I set it to
"Mahomes, KC". I read it into the script something like this:
thisPick =  name of the property field .evaluateAttributeExpressions().getValue()

My first question: can I reference that incoming value in this expression directly by variable
reference without quoting as above, and is there a way I can auto detect the property name
to use as my key value in my dictionary?
result[name of the property field] = flowFile.getAttribute(thisPick)

My second question: is there a way to generalize this so that it dynamically identifies and
handles any variable number of properties and values defined in my ExecuteScript processor?
This would be the ideal solution, because then I can have one python script that dynamically
handles any number of  properties I set in the processor. I would do that to grab whichever
subset of attributes I happened to want to send to my AMQP as a message at that time.

Jim

Mime
View raw message