Matt,
Thank you so much for your suggestion.But I would like to go with
executescript since I'm almost done with the code.I will try the processors
which you told in the future
I'm still stuck with some problem in my code.I have added it here
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import unicodecsv as csv
from faker import Factory
from collections import defaultdict
import json
import csv
import io
class TransformCallback(StreamCallback):
def _init_(self):
pass
def process(self,inputStream,outputStream):
inputdata =
IOUtils.toString(inputStream,StandardCharsets.ISO_8859_1)
text = csv.DictReader(io.StringIO(inputdata))
textdata = list(text)
length = len(textdata)
outputstr = '['
i = 1
faker = Factory.create()
names = defaultdict(faker.name)
emails = defaultdict(faker.email)
ssns = defaultdict(faker.ssn)
phone_numbers = defaultdict(faker.phone_number)
output = defaultdict(list)
for row in text:
for k,v in row.items():
if k == "name":
output['name'] = names[v]
elif k == "email":
output['email'] = emails[v]
elif k == "ssn":
output['ssn'] = ssns[v]
elif k == "phone_number":
output['phone_number'] = phone_numbers[v]
else:
output[k] = v
outputstr += json.dumps(output)
if i == length:
outputstr += json.dumps(output)
if i == length:
outputstr = outputstr+']'
else:
outputstr += ','
i = i+1
outputstr = json.dumps(output)
outputStream.write(outputstr.encode('utf-8'))
flowFile = session.get()
if flowFile != None:
flowFile = session.write(flowFile,TransformCallback())
session.transfer(flowFile, REL_SUCCESS)
session.commit()
I'm append each "output" instance of dictionary to "outputstr" so that I
could write it to the flowfile.I'm setting up "outputstr" in such a way it
is in json format.So,the expected output would be
[{"phone_number": "(620)790-6114x4000", "ssn": "575-97-5718", "email":
"[email protected]", "name": "Kenneth Bradley"},{"phone_number":
"(000)790-6114x4000", "ssn": "470-97-5718", "email": "[email protected]",
"name": "Romeo Bradley"}]
But the output I get is "["
There is some problem in "output" getting appended to "outputstr"
Please help me with appropriate suggestion.I'm not able to figure out where
I have gone wrong.
Thanks,
Vyshali
--
Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/