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

'SyntaxError: mismatched input' when reading attributes from flowFile on ExecuteScriptProcessor

Contributor

I am using an ExecuteScript processor to read the keys and values of all the attributes from a 0 byte empty flowFile into a JSON string, write the JSON string to the contents of the flowFile and transfer to REL_SUCCESS. This is the script in the ExecuteScriptProcessor:

from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import OutputStreamCallback

# Define a subclass of OutputStreamCallback for use in session.write()
class PyOutputStreamCallback(OutputStreamCallback):
    def __init__(self):
        pass

    def process(self, outputStream):
        atts = {}

        for key, value in flowFile.getAttributes().iteritems():
            atts[key] = str(value).encode('utf-8')
        atts_string = str(atts)

        outputStream.write(bytearray(atts_string.encode('utf-8')))
# end class

flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile, PyOutputStreamCallback())
    session.transfer(flowFile, REL_SUCCESS)
# implicit return at the end

I have been using GenerateFlowFile and UpdateAttributes to generate a test flowFile, the info for which is the image attached to this post.

When I try to run the pipeline, I get this output:

SyntaxError: mismatched input '/' expecting EOF in <script> at line number 1 at column number 0

From what I've found on Google, this error typically means an indenting or formatting issue with Python, but I've made absolutely sure that is not the case by deleting and re-tabbing each line individually since its such a short script, but the error still persists.

So it seems to be NiFi related but I have no idea what could be causing it, I can't find a '/' character anywhere in the sample flowFile attributes (the 'path' attribute had a '/' but I changed it to 'test' and the issue persisted) or my code.

EDIT:

I just realized there is an AttributesToJson processor that I can use to keep going, but I am still curious as to why my code above isn't working, I feel like this is a use case similar to things I will want to do in the future and it makes me nervous not understanding why this won't work.


sample-flowfile.png
0 REPLIES 0