2
votes

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.

2

2 Answers

3
votes

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.

2
votes

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