Member since
10-20-2017
3
Posts
0
Kudos Received
0
Solutions
10-20-2017
09:21 PM
Answered my own question. This is a nice way of doing it I think! (didn't include imports or comments to save a few lines) class PyStreamCallback(StreamCallback):
def __init__(self):
# init stuff, or just pass
pass
def process(self, inputStream, outputStream):
inputText = IOUtils.toString(inputStream, StandardCharsets.ISO_8859_1)
self.thingIWantToPutInAnAttribute = really_important_function(inputText)
def thing(self): return self.thingIWantToPutInAnAttribute
flowFile = session.get()
if (flowFile!=None):
reader = PyStreamCallback()
flowFile = session.write(flowFile,reader)
flowFile = session.putAttribute(flowFile,'ThisIsAnAttributeName',reader.thing())
session.transfer(flowFile, REL_SUCCESS)
... View more
10-20-2017
07:29 PM
I suppose an additional question; I assume pulling in >1 flowfiles reduces the overhead of importing packages in the executeScript process... Not too sure how true this is. True? Not true?
... View more
10-20-2017
07:24 PM
Hi Matt! These tutorials have been incredibly useful for learning how to write ExecuteScript processors; jython in particular for me. One thing I'd love to hear from you on though: How do we read the contents of a flowfile, then create attributes with this information? I've created flowFile IO scripts no problem, and I've manipulated attributes without a problem, but I'm not sure how to make something that crosses the barrier, so to speak. I'll clarify with a short snippet of code below. Any help is greatly appreciated! from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import A FEW IMPORTANT THINGS
class PyStreamCallback(StreamCallback):
def __init__(self):
# init stuff, or just pass
pass
def process(self, inputStream, outputStream):
inputText = IOUtils.toString(inputStream, StandardCharsets.ISO_8859_1)
# Process inputText
thingIWantToPutInAnAttribute = inputText[0:10]
payload = ''' Processing inputText... '''
# to my knowledge I'm unable to write to a flowFile attribute from within this class.
# I could make it a class variable with self.thing = thingIWantToPutInAnAttribute
# but then I'm unaware how I can access it outside the class.
# Write to file
outputStream.write(bytearray(payload.encode('ascii','ignore')))
flowFile = session.get(100)
if (flowFile!=None):
flowFile = session.write(flowFile,PyStreamCallback())
# How do I get thingIWantToPutInAnAttribute here?
session.transfer(flowFile, REL_SUCCESS)
... View more