Created 07-01-2022 01:00 AM
I am learning the ExecuteScript processor. I want to understand, how to use python script to fetch a file from local, similar to what the GetFile processor does.
Thanks
Created 07-01-2022 02:55 AM
I think you should refer to Ni-Fi cookbook
That is pretty much THE only instruction for making scripts for ni fi
Created 07-01-2022 02:55 AM
I think you should refer to Ni-Fi cookbook
That is pretty much THE only instruction for making scripts for ni fi
Created on 07-04-2022 02:00 AM - edited 07-04-2022 07:02 AM
Thanks. Using examples from that cookbook, I could make it work.
Here is my code which works:
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import OutputStreamCallback
class PyOutputStreamCallback(OutputStreamCallback):
def __init__(self):
pass
def process(self, outputStream):
with open("D:\\Work\\nifi test\\custom processor input\\random_json.json") as f:
file_content = f.read()
outputStream.write(bytearray(file_content.encode('utf-8')))
flowFile = session.create()
if(flowFile != None):
flowFile = session.write(flowFile, PyOutputStreamCallback())
flowFile = session.putAttribute(flowFile, "filename", 'input_file.json')
session.transfer(flowFile, REL_SUCCESS)
session.commit()
Next, I will figure out if I can add the local directory path as a property and read that, instead of hardcoding it in the script.
Created 07-01-2022 05:50 AM
hi,
Is there a reason that you want to create your own fetch files processor vs using the existing processors like: GetFile or FetchFile
Created 07-01-2022 06:25 AM
As part of a POC, we want to explore the capabilities of a custom script to fetch files from a source, that pre-existing processors cannot. As a starting step, we are trying to try out writing a script that can fetch file similar to GetFile, to get some basic idea of how that would work. Then we will try out more complicated sources, of which nifi perhaps doesn't provide support by default.
Created 07-01-2022 06:43 AM
If you want you can take a a look at the actual source code for existing procssors like GetFile on Github to see if that will help: