I try to read from a flowfile and update a record value using default value in csv. To that I have used ExecuteScript processor with following python code in it.
import sys
import re
import traceback
from org.apache.commons.io import IOUtils
from org.apache.nifi.processor.io import StreamCallback
from org.python.core.util import StringUtil
from java.lang import Class
from java.io import BufferedReader
from java.io import InputStreamReader
from java.io import OutputStreamWriter
flowfile = session.get()
record = flowfile.getAttribute('record_type')
if record == '0':
flowfile = session.putAttribute(flowfile,'record_type', 'NEW_USER')
session.transfer(flowFile, REL_SUCCESS)
session.commit()
elif record == '1':
flowfile = session.putAttribute(flowfile,'record_type', 'OLD_USER')
session.transfer(flowFile, REL_SUCCESS)
session.commit()
else:
flowfile = session.putAttribute(flowfile,'record_type', 'IGNORE')
session.transfer(flowFile, REL_SUCCESS)
session.commit()
writer.flush()
writer.close()
reader.close()
My csv looks like
id,record_type
1,0
2,1
3,2
4,0
Result should be :
id,record_type
1,NEW_USER
2,OLD_USER
3,IGNORE
4,NEW_USER
I get following error :
AttributeError : 'NoneType' object has no attribute 'getAttribute' in script at line number 13
It says record = flowfile.getAttribute('record_type') this is wrong..
I have no idea how to solve this as I am not good with python.
flowFile.writefunction. Search the inet fornifi python cookbookand look at the examples.UpdateRecordprocessor, but having issue with replacing multiple values in one step as described in the question.ifand I think it's possible to use UpdateRecord in your case. I could show how to do groovy script for your case.. (I'm bad in python also)