I am working on a Nifi flow where I am getting a JSON document with multiple key-value pairs. I am using the ExecuteScript processor with python.
My goal here is to create various URLS bases on JSON keys. The keys are numerical and they look like this:
keys = [10200, 10201, 10202, ...]
The URLs I want are of 3 types and they should look like these:
http://google.com/10200
http://bing.com/10200
http://yahoo.com/10200
I am trying to loop through my keys[] and create 3 specific urls for each numerical keys that it contains. I have the following code where I am trying to:
read a numerical key from list --> create 3 URLs --> spit out a flow file.
...... and read the next numerical key in the list and keep looping.....
I have the following code but when I give it a JSON flowfile it does not do anything right now. Can someone please tell me what I am doing wrong?
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
class ModJSON(StreamCallback):
def __init__(self):
self.parentFlowFile = None
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
obj = json.loads(text)
flowfiles_list = []
outputStream.write(bytearray(json.dumps(obj.keys(), indent=4).encode('utf-8')))
for numerical_key in obj.keys():
# create 1 flowfile for each numerical_key. Each flow file should have 3 url attributes
flowFile = session.create(self.parentFlowFile)
if (flowFile != None):
flowFile = session.write(flowFile, "Does not matter")
flowFile = session.putAttribute(flowFile, "google", "http://google.com/"+ numerical_key)
flowFile = session.putAttribute(flowFile, "google", "http://bing.com/"+ numerical_key)
flowFile = session.putAttribute(flowFile, "google", "http://yahoo.com/"+ numerical_key)
flowfiles_list.append(flowFile)
for flow in flowfiles_list:
session.transfer(flow, REL_SUCCESS)