Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How to use ExecuteScript (with python as a script engine) for an exercise to add numbers? [Novice user trying to learn NiFi]

avatar
New Contributor

I am relatively new to NiFi and am not sure how to do the following correctly. I would like to use ExecuteScript processor (script engine: python) to do the following (only in python please):

1) There is a CSV file containing the following information (the first row is the header):

first,second,third

1,4,9
7,5,2
3,8,7

2) I would like to find the sum of individual rows and generate a final file with a modified header. The final file should look like this:

first,second,third,total

1,4,9,14
7,5,2,14
3,8,7,18 

For the python script, I wrote:

def summation(first,second,third):
    numbers = first + second + third
    return numbers
flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile, summation())

But it does not work and I am not sure how to fix this. Can anyone provide me an understanding on how to approach this problem?

The NiFi flow:

79545-nififlow.png

1 REPLY 1

avatar
Super Guru

First, congrats on getting started with NiFi and not being afraid to tackle one of the harder processors: ExecuteScript.

My advice is to start small, get a working script that maybe doesnt do what you want, but does actually complete and return SUCCESS in your NiFi flow. From there you can start building it up and testing step by step. I also recommend that you tail the NIFI Log during all of your work so that you can see any errors or information from each test/step. My last peice of advice for an advanced script would be to manually execute your script to ensure that its logic is working outside of the execution from within NiFi.

You may find the following links very helpful:

https://community.hortonworks.com/articles/75032/executescript-cookbook-part-1.html
https://community.hortonworks.com/articles/75545/executescript-cookbook-part-2.html
https://community.hortonworks.com/articles/75545/executescript-cookbook-part-3.html

If my answer is helpful, please choose ACCEPT.