Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file content into nifi attribute using executescript

I'm trying to load the contents of a file into an attribute and preface it with "Bearer ". This is how far I got:

def flowFile = session.get();
if (flowFile != null) {

    def token = flowFile.getAttribute("token")
    def message = "Bearer " + token
    flowFile = session.putAttribute(flowFile, "message", message)
    session.transfer(flowFile, REL_SUCCESS)
}

However I'm stuck trying to load file contents rather than using .getattribute. Can anyone please help me?

EDIT: To confirm, this is to load the contents of a txt file.

like image 965
J.Zil Avatar asked May 10 '26 02:05

J.Zil


1 Answers

you could use ExecuteGroovyScript with following code to read flowfile content and put it into an attribute:

def ff = session.get()
if(!ff)return

ff.message = "Bearer " + ff.read().getText("UTF-8")
REL_SUCCESS << ff

to read usual file just replace ff.read().getText("UTF-8")

with new File("path/to/file.txt").getText("UTF-8")


NOTE: beware to store large values into attributes.

like image 83
daggett Avatar answered May 14 '26 00:05

daggett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!