azelix
azelix

Reputation: 1277

Use FlowFile attributes in a executeScript NIFI python

I'm trying to get attributes of a flowfile in my python script , I have done the following :

class TransformCallback(StreamCallback):

    def __init__(self):
        pass

    def process(self, inputStream, outputStream):
        try:
            # Read input FlowFile content
            input_text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
            input_obj = json.loads(input_text)

but how can I access my flowfile attributes in the process method ?

Upvotes: 2

Views: 1801

Answers (1)

mattyb
mattyb

Reputation: 12093

They won't be immediately available in the process method unless you do something like pass a reference to the FlowFile into your TransformCallback constructor. Another option is to split up the reading and writing (since you are using IOUtils.toString() to read the whole thing in at once) into two separate calls, then you can do the attribute manipulation outside the process() methods.

By the way, if you just need to read in the whole content as a string, you don't need a StreamCallback or InputStreamCallback, you can use session.read(flowFile) which returns an InputStream (rather than executing a provided callback). You can call IOUtils.toString() on that (and don't forget to close it afterwards), thereby avoiding the callback and allowing easier access to the flow file attributes using your current FlowFile reference (and the getAttribute() or getAttributes() methods).

Upvotes: 2

Related Questions