I have been trying to extract data from nifi's ExecuteScript Processor and attach to the flowfile as attributes. I have tried many sources out there, especially the one at funnifi blog of Matt Burgess.
Following is my code
import json
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
originalFlowFile = session.get()
text = IOUtils.toString(originalFlowFile)
log.info(text)
if(originalFlowFile != None):
event = json.loads(text)
if (event['true'] == 'Y'):
flowfile = session.putAttribute(flowfile, "true", "Y")
elif (event['src'] == 'ONE' ):
allAttributes = { "true": "N", "src": "ONE" }
flowfile = session.putAllAttributes(flowfile, allAttributes)
elif (event['src'] == 'TWO' ):
allAttributes = { "true": "N", "src": "TWO" }
flowfile = session.putAllAttributes(flowfile, allAttributes)
session.transfer(flowFile, REL_SUCCESS)
session.commit()
This is in python. The flowfile data IS a JSON. Still, I am unable to parse it.
The INFO part is the output of
text = IOUtils.toString(originalFlowFile)
Any help would be greatly appreciated.
p.s. I am unfamiliar with python
Test data
{
"true":"N",
"src":"ONE",
"var1":"value1",
"var2":"value2"
}
UPDATE
My updated code, which still does not work:
import json
import java.io
from org.apache.commons.io import IOUtils
originalFlowFile = session.get()
if(originalFlowFile != None):
inputStream = session.read(originalFlowFile)
text = IOUtils.toString(inputStream)
log.info(text)
event = json.loads(text)
if (event['true'] == 'Y'):
flowfile = session.putAttribute(flowfile, "true", "Y")
elif (event['src'] == 'ONE' ):
allAttributes = { "true": "N", "src": "ONE" }
flowfile = session.putAllAttributes(flowfile, allAttributes)
elif (event['src'] == 'TWO' ):
allAttributes = { "true": "N", "src": "TWO" }
flowfile = session.putAllAttributes(flowfile, allAttributes)
session.transfer(flowFile, REL_SUCCESS)
session.commit()

no json object could be decodedmeans you don't have a json object in flow file content. could you update your question and add: 1) a flowfile content. 2) a goal what you want to do with your script. and why you choose python when you unfamiliar with it? - daggett