Skip to main content
sayak17
Gold ⭐️
July 1, 2022
Solved

python script to fetch files in NiFi

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

Brenigan
BreniganAnswer
Gold ⭐️
July 1, 2022

I think you should refer to Ni-Fi cookbook
That is pretty much THE only instruction for making scripts for ni fi

5 replies

Brenigan
BreniganAnswer
Gold ⭐️
July 1, 2022

I think you should refer to Ni-Fi cookbook
That is pretty much THE only instruction for making scripts for ni fi

Translate
sayak17
sayak17Author
Gold ⭐️
July 4, 2022

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.

Translate
SAMSAL
Platinum ⭐️⭐️⭐️
July 1, 2022

hi,

Is there a reason that you want to create your own fetch files processor vs using the existing processors like: GetFile or FetchFile

Translate
sayak17
sayak17Author
Gold ⭐️
July 1, 2022

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.

Translate
SAMSAL
Platinum ⭐️⭐️⭐️
July 1, 2022

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:

https://github.com/coco11563/nifi-1.4.0/blob/master/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/GetFile.java

 

Translate