Forked from ijokarumawak/read-flowfile-contents.py
Created
September 1, 2017 20:16
-
-
Save robenalt/e63a843989561826b73b2d37ae8ea960 to your computer and use it in GitHub Desktop.
Example Python script to use from NiFi ExecuteScript processor which reads the first line from an incoming flow file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from org.apache.nifi.processors.script import ExecuteScript | |
from org.apache.nifi.processor.io import InputStreamCallback | |
from java.io import BufferedReader, InputStreamReader | |
class ReadFirstLine(InputStreamCallback) : | |
__line = None; | |
def __init__(self) : | |
pass | |
def getLine(self) : | |
return self.__line | |
def process(self, input) : | |
try : | |
reader = InputStreamReader(input) | |
bufferedReader = BufferedReader(reader) | |
self.__line = bufferedReader.readLine() | |
except : | |
print "Exception in Reader:" | |
print '-' * 60 | |
traceback.print_exc(file=sys.stdout) | |
print '-' * 60 | |
raise | |
finally : | |
if bufferedReader is not None : | |
bufferedReader.close() | |
if reader is not None : | |
reader.close() | |
flowFile = session.get() | |
if flowFile is not None : | |
reader = ReadFirstLine() | |
session.read(flowFile, reader) | |
flowFile = session.putAttribute(flowFile, "from-content", reader.getLine()) | |
session.transfer(flowFile, ExecuteScript.REL_SUCCESS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment