You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nifi.apache.org by Max Viazovskyi <il...@gmail.com> on 2018/04/22 08:18:18 UTC

ExecuteGroovyScript processor unable to resolve IOUtils

Recently I needed to write custom script to override flow file content, when
script was prepared I fount that it can be executed with ExecuteScript
processor, but ExecuteGroovyScript fails to compile the same script, it
shows the error: unable to resolve class import
org.apache.commons.io.IOUtils.
<http://apache-nifi-developer-list.39713.n7.nabble.com/file/t949/groovy_script_error.png> 

Script is the following:
import org.apache.commons.io.IOUtils
import java.nio.charset.*

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


flowFile = session.write(flowFile,
    { inputStream, outputStream ->
        def text = org.apache.commons.io.IOUtils.toString(inputStream,
StandardCharsets.UTF_8)
        text = text + '\n' + new Date()
        outputStream.write(text.getBytes(StandardCharsets.UTF_8))
    } as StreamCallback)

session.transfer(flowFile, ExecuteScript.REL_SUCCESS)

Also you could check it with template  GroovyScriptIsInvalid.xml
<http://apache-nifi-developer-list.39713.n7.nabble.com/file/t949/GroovyScriptIsInvalid.xml>  

Thanks,
Max



--
Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/

Re: ExecuteGroovyScript processor unable to resolve IOUtils

Posted by Matt Burgess <ma...@apache.org>.
Max,

ExecuteGroovyScript does not (yet) automatically include some Apache
Commons libraries such as commons-lang3, commons-io, commons-net, etc.
However ExecuteScript does already include those, so you can put your
Groovy script in there and not have to @Grab the library.  Of course,
you won't get all the sweet idiomatic Groovyness of
ExecuteGroovyScript, but you won't have to worry about Grapes or the
file system or any other possible issues with @Grab.

Please feel free to file a Jira to add Apache Commons libraries to the
ExecuteGroovyScript NAR, I'd be happy to code up the fix :)

Regards,
Matt


On Sun, Apr 22, 2018 at 10:56 AM, Max Viazovskyi <il...@gmail.com> wrote:
> Thanks Andrew, I'm using dockerized NiFi 1.6.0. I'm new for groovy and Java,
> could you please guide how should this annotation looks like? I believe
> something like:
> @Grab(group='commons-io', module='commons-io', version='2.7-SNAPSHOT')
> import org.apache.commons.io.IOUtils
>
> Max
>
>
>
> --
> Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/

Re: ExecuteGroovyScript processor unable to resolve IOUtils

Posted by Max Viazovskyi <il...@gmail.com>.
Thanks Andrew, I'm using dockerized NiFi 1.6.0. I'm new for groovy and Java,
could you please guide how should this annotation looks like? I believe
something like:
@Grab(group='commons-io', module='commons-io', version='2.7-SNAPSHOT')
import org.apache.commons.io.IOUtils 

Max



--
Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/

Re: ExecuteGroovyScript processor unable to resolve IOUtils

Posted by Andrew Grande <ap...@gmail.com>.
Max, which NiFi version are you using. Can you try adding the @Grab
annotation in your script declaring the commons-io dependency? IIRC, it was
added recently.

Andrew

On Sun, Apr 22, 2018, 7:38 AM Max Viazovskyi <il...@gmail.com> wrote:

> Recently I needed to write custom script to override flow file content,
> when
> script was prepared I fount that it can be executed with ExecuteScript
> processor, but ExecuteGroovyScript fails to compile the same script, it
> shows the error: unable to resolve class import
> org.apache.commons.io.IOUtils.
> <
> http://apache-nifi-developer-list.39713.n7.nabble.com/file/t949/groovy_script_error.png>
>
>
> Script is the following:
> import org.apache.commons.io.IOUtils
> import java.nio.charset.*
>
> def flowFile = session.get()
> if (!flowFile) return
>
>
> flowFile = session.write(flowFile,
>     { inputStream, outputStream ->
>         def text = org.apache.commons.io.IOUtils.toString(inputStream,
> StandardCharsets.UTF_8)
>         text = text + '\n' + new Date()
>         outputStream.write(text.getBytes(StandardCharsets.UTF_8))
>     } as StreamCallback)
>
> session.transfer(flowFile, ExecuteScript.REL_SUCCESS)
>
> Also you could check it with template  GroovyScriptIsInvalid.xml
> <
> http://apache-nifi-developer-list.39713.n7.nabble.com/file/t949/GroovyScriptIsInvalid.xml>
>
>
> Thanks,
> Max
>
>
>
> --
> Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/
>