Andy,

Thank you for your detailed example. Unfortunately I don’t know Groovy though 
from your example I see that it’s syntax is Scala-like and rather transparent.
My preference at this point is to use pure Java, if possible.
I managed to get my Java class executed by composing the following pipeline.
GenerateFlowFile -> ExecuteStreamCommand -> PutFile
                                                          |
                                                         \/
                                           jar with custom Java class

The hardest part was figuring out that I must place GenerateFlowFile processor 
before ExecuteStreamCommand in order for it to work.
While documentation of ExecuteStreamCommand processor says that it must have an 
incoming connection, it says nothing about how to go about creating it. This is 
especially counterintuitive when a custom Java class jar doesn’t need any input 
from NiFi flow. It’s a kind of a situation where one really wishes for a 
cookbook on NiFi.

From: Andy LoPresto [mailto:alopre...@apache.org]
Sent: Monday, July 16, 2018 1:21 PM
To: users@nifi.apache.org
Subject: Re: NiFi processor to execute a Java class

You can execute arbitrary Java logic via ExecuteScript and 
InvokeScriptedProcessor as well. Simply import the relevant Java class as 
usual, and invoke the methods as you would in any other scenario. It will be 
easiest to do this with Groovy as your scripting language, and ensure that the 
JAR containing the classes is available in the “modules” directory configured 
on the processor.



import org.apache.commons.io.IOUtils

import java.nio.charset.*

import com.jhancock.CustomService



def flowFile = session.get()

if(!flowFile) return



flowFile = session.write(flowFile, {inputStream, outputStream ->

   // For demo purpose only; this is not memory-nice

   def flowFileContents = IOUtils.toString(inputStream, StandardCharsets.UTF_8)



   // Do something with your service

   CustomService myCustomService = new CustomService()

   def result = myCustomService.doSomething(flowFileContents)



   outputStream.write(result.getBytes(StandardCharsets.UTF_8))

} as StreamCallback)



flowFile = session.putAttribute(flowFile, ‘custom_service_performed', ’true')

session.transfer(flowFile, REL_SUCCESS)





Andy LoPresto
alopre...@apache.org<mailto:alopre...@apache.org>
alopresto.apa...@gmail.com<mailto:alopresto.apa...@gmail.com>
PGP Fingerprint: 70EC B3E5 98A6 5A3F D3C4  BACE 3C6E F65B 2F7D EF69

On Jul 16, 2018, at 8:23 AM, Mike Thomsen 
<mikerthom...@gmail.com<mailto:mikerthom...@gmail.com>> wrote:

As far as I know, it'll capture the stdout/stderr output of your Java class and 
send that to a flowfile. Anything beyond that you'd need to do yourself.

On Mon, Jul 16, 2018 at 11:18 AM Vitaly Krivoy 
<vitaly_kri...@jhancock.com<mailto:vitaly_kri...@jhancock.com>> wrote:
Thank you Mike. The objective of my custom class is to query data from 
salesforce. Will ExecuteStreamCommand processor automatically capture the 
output of my custom Java class and generate flow files, or do I have to 
implement this capability myself?

From: Mike Thomsen 
[mailto:mikerthom...@gmail.com<mailto:mikerthom...@gmail.com>]
Sent: Monday, July 16, 2018 11:07 AM
To: users@nifi.apache.org<mailto:users@nifi.apache.org>
Subject: Re: NiFi processor to execute a Java class

The REPL is not going to help unless it provides a ScriptEngine implementation, 
which I don't believe it does. At this point, I think your only option between 
the three is ExecuteStreamCommand.

On Mon, Jul 16, 2018 at 10:54 AM Vitaly Krivoy 
<vitaly_kri...@jhancock.com<mailto:vitaly_kri...@jhancock.com>> wrote:
What would be the best NiFi processor to execute a Java class? I’ve seen some 
references to ExecuteScript, InvokeScriptedProcessor and ExecuteStreamCommand 
processors, but nothing conclusive. Speaking of ExecuteScript processor - would 
it work with Java 8, or should it be used with Java 9 or later because those 
releases have a REPL loop?  Thanks.

Vitaly Krivoy

STATEMENT OF CONFIDENTIALITY The information contained in this email message 
and any attachments may be confidential and legally privileged and is intended 
for the use of the addressee(s) only. If you are not an intended recipient, 
please: (1) notify me immediately by replying to this message; (2) do not use, 
disseminate, distribute or reproduce any part of the message or any attachment; 
and (3) destroy all copies of this message and any attachments.

STATEMENT OF CONFIDENTIALITY The information contained in this email message 
and any attachments may be confidential and legally privileged and is intended 
for the use of the addressee(s) only. If you are not an intended recipient, 
please: (1) notify me immediately by replying to this message; (2) do not use, 
disseminate, distribute or reproduce any part of the message or any attachment; 
and (3) destroy all copies of this message and any attachments.


STATEMENT OF CONFIDENTIALITY The information contained in this email message 
and any attachments may be confidential and legally privileged and is intended 
for the use of the addressee(s) only. If you are not an intended recipient, 
please: (1) notify me immediately by replying to this message; (2) do not use, 
disseminate, distribute or reproduce any part of the message or any attachment; 
and (3) destroy all copies of this message and any attachments.

Reply via email to