Kotlin is an excellent choice for developing JVM-based applications, including
extensions for Apache NiFi. Writing custom NiFi Processors in Kotlin minimizes
boilerplate code and enhances null safety, reducing the likelihood of bugs.
Additionally, NiFi's lambda callback conventions align seamlessly with Kotlin's
trailing lambda syntax. Let's dive in.
Below is a basic implementation of the AbstractProcessor interface.
This processor transfers incoming FlowFiles to the success relationship.
Obviously, we can make this more interesting. Let's implement onTrigger to
return a paragraph of text per incoming FlowFile.
First, let's add a new PropertyDescriptor so that the User can decide which
paragraph they want.
Then, let's modify onTrigger to do something interesting.
This method does the following:
Read the incoming FlowFile content
Split on blank lines
Select the element based on the index property
Replace the FlowFile content with the selection
For example, if the incoming FlowFile has contents:
With the index property set to 1, the FlowFile's content will change to that
of From the reputation and remembrance of my father, modesty and a manly character.
Writing Tests
We can easily test this processor using NiFi's TestRunner.
Registering your Processor
In order for NiFi to recognize your Processor you must register it in a special resource file: nifi-starter-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor
The contents of this file are the import paths of your processors. In our case, it will be one line for ExampleProcessor.
Building a NAR file
We can use Maven to package our Kotlin Processors as a NAR file ("NiFi
Archive"). NAR files are NiFi's preferred extension format as they provide
dependency isolation between different Processors. It's a good idea to organize
your project into a two modules: one for source code, and one for NAR bundling.
We will be creating three pom.xml files in total: the root pom, the kotlin
code pom, and the NAR bundler pom.
And finally, the root pom.xml
We have created three projects:
Root project
Processor project
NAR project
To build a NAR bundle that NiFi can read, we can call maven in the root
directory with mvn package.
Your NAR file will be at nifi-starter-nar/target/nifi-starter-nar-0.0.1.nar.
Add this file to NiFi's /opt/nifi/nifi-current/nar_extensions directory in
order to use your new Processor. We can now use ExampleProcessor in our flows.
Apache NiFi is a great computing platform, and using Kotlin to create custom
Processors unlocks many interesting engineering possibilities.