Support Questions
Find answers, ask questions, and share your expertise

Apache Nifi ScriptedReader issue with session

Apache Nifi ScriptedReader issue with session

New Contributor

Hi all.

Trying to create a ScriptedReader controller service for a ConvertRecord, when enabling it, it always raises the following error, complaining about the session object:

 

 javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: session for class: Script205

I'm using a quite basic Groovy script to read and write the flowfile:

 

 

 

import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
def flowFile = session.get();
if (flowFile == null) {
    return;
}
// This uses a closure acting as a StreamCallback to do the writing of the new content to the flowfile
flowFile = session.write(flowFile,
        { inputStream, outputStream ->
            String line

            // This code creates a buffered reader over the existing flowfile input
            final BufferedReader inReader = new BufferedReader(new InputStreamReader(inputStream, 'UTF-8'))

            // For each line, write the reversed line to the output
            while (line = inReader.readLine()) {
                outputStream.write("${line.reverse()}\n".getBytes('UTF-8'))
            }
        } as StreamCallback)

flowFile = session.putAttribute(flowFile, "reversed_lines", "true")
session.transfer(flowFile, /*ExecuteScript.*/ REL_SUCCESS)

 

 

 

 

Could anyone help me to understand why that session object is not recognizable, while is perfectly recognizable from a ExecuteScript processor?

Thanks in advance