J.Zil
J.Zil

Reputation: 2449

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.

Upvotes: 3

Views: 2153

Answers (2)

daggett
daggett

Reputation: 28634

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.

Upvotes: 4

Bryan Bende
Bryan Bende

Reputation: 18670

You need to call session.read with the flow file and an InputStreamCallback.

There is an example here:

https://funnifi.blogspot.com/2016/08/executing-remote-commands-in-nifi-with.html

Upvotes: 2

Related Questions