Created on 06-17-2019 11:37 PM - edited 08-17-2019 02:55 PM
HI All, I am trying to iterate the nifi flow file and modify each line in the flow file using python script in 'ExecuteScript'.But it is not working as expected. For example i would like to append 'Rajiv' string at the end for each line in the flow file.but it is adding only at the end of the end of the flow file.
Code
Execute script content:
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 PyStreamCallback(StreamCallback):
def __init__(self, flowfile):
self.ff = flowfile
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
text = text + 'rajiv'
outputStream.write(bytearray(text.encode('utf-8')))
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile,PyStreamCallback(flowFile))
session.transfer(flowFile, REL_SUCCESS)
Generate flow file input:
1234
5678
output:
1234
5678rajiv
Expected output:
1234rajiv
5678rajiv
Created 06-18-2019 01:08 AM
Please let me know if the question is not clear. I will re-write the question again.
Created 10-11-2019 07:15 AM
Hi,
try :
text = text.replace('\n','rajiv')
instead of
text = text + 'rajiv'
maddalaus
Created on 10-11-2019 07:56 AM - edited 10-11-2019 07:57 AM
Hi @rajivswe_2k7,
Alternatively, you can run your FlowFile through a ReplaceText processor and configuring it with replacement value of "ravi" / replacement strategy of "append" and Evaluation mode of "Line-by-line"