Package Java Functions
There are two methods to package Java Functions, that is uber JAR and NAR.
If you plan to package and distribute your function for others to use, you are obligated to license and copyright your own code properly. Remember to add the license and copyright to all libraries your code uses and to your distribution.
If you use the NAR method, the NAR plugin
automatically creates a DEPENDENCIES
file in the generated NAR package, including the proper
licensing and copyrights of all libraries of your function.
For the runtime Java version, refer to Pulsar Runtime Java Version Recommendation according to your target Pulsar version.
Package as JAR
To package a Java function as JAR, complete the following steps.
-
Create a new maven project with a pom file. In the following code sample, the value of
mainClass
is your package name.<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>java-function</groupId>
<artifactId>java-function</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-functions-api</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.example.test.ExclamationFunction</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
</project> -
Package your Java function.
mvn package
After the Java function is packaged, a
target
directory is created automatically. Open thetarget
directory to check if there is a JAR package similar tojava-function-1.0-SNAPSHOT.jar
. -
Copy the packaged jar file to the Pulsar image.
docker exec -it [CONTAINER ID] /bin/bash
docker cp <path of java-function-1.0-SNAPSHOT.jar> CONTAINER ID:/pulsar -
Run the Java function using the following command.
./bin/pulsar-admin functions localrun \
--classname org.example.test.ExclamationFunction \
--jar java-function-1.0-SNAPSHOT.jar \
--inputs persistent://public/default/my-topic-1 \
--output persistent://public/default/test-1 \
--tenant public \
--namespace default \
--name JavaFunctionThe following log indicates that the Java function starts successfully.
...
07:55:03.724 [main] INFO org.apache.pulsar.functions.runtime.ProcessRuntime - Started process successfully
...
Package as NAR
To package a Java function as NAR, complete the following steps.
-
Create a new maven project with a pom file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>java-function</groupId>
<artifactId>java-function</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-functions-api</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-nar-maven-plugin</artifactId>
<version>1.5.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
</project>You must also create a
resources/META-INF/services/pulsar-io.yaml
file. In the following code sample, the value offunctionClass
is your function class name. Thename
is the one used when the Function is deployed as a built-in one.name: java-function
description: my java function
functionClass: org.example.test.ExclamationFunction -
Package your Java function.
mvn package
After the Java function is packaged, a
target
directory is created automatically. Open thetarget
directory to check if there is a NAR package similar tojava-function-1.0-SNAPSHOT.nar
. -
Copy the packaged NAR file to the Pulsar image.
docker cp <path of java-function-1.0-SNAPSHOT.nar> CONTAINER ID:/pulsar
-
Run the Java function using the following command.
./bin/pulsar-admin functions localrun \
--jar java-function-1.0-SNAPSHOT.nar \
--inputs persistent://public/default/my-topic-1 \
--output persistent://public/default/test-1 \
--tenant public \
--namespace default \
--name JavaFunctionThe following log indicates that the Java function starts successfully.
...
07:55:03.724 [main] INFO org.apache.pulsar.functions.runtime.ProcessRuntime - Started process successfully
...