Running locally, this works exactly how I want it to (has one incoming flow file with many different codes in position 7-10 and outputs 1 file per unique code) For example if record 1-5 has 1234 in positions 7-10, and record 6 has 2345 in position 7-10, and record 7 has 1234 in positions 7-10, then there would be one file called 1234_file.txt with rows 1-5 and 7 and a second file 2345_file.txt would have row 6 from the input file:
f = open("test_comp.txt", "r")
for x in f:
comp = x[6:10]
print(comp)
n = open(comp+"_file.txt","a")
n.write(x)
n.close()
f.close()
In nifi, I am trying this:
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):
pass
def process(self, inputStream, outputStream):
f = open(inputStream, 'r')
for x in f:
comp = x[6:10]
print("comp: ",comp)
newFile = open(comp+"_file.txt","a")
newFile.write(x)
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile, PyStreamCallback())
session.transfer(flowFile, REL_SUCCESS)
session.commit()
It seems to be getting the input and correctly storing comp as position 7-10 as expected, but I'm not getting multiple flow files out (for each unique string in x[6:10]. And the flow file coming out is 1 zero byte file.
Any thoughts on what I'm missing??