Package Java Functions

There are two methods to package Java Functions, that is uber JAR and NAR.

Package Java Functions - 图1note

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.

  1. Create a new maven project with a pom file. In the following code sample, the value of mainClass is your package name.

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>java-function</groupId>
    7. <artifactId>java-function</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.apache.pulsar</groupId>
    12. <artifactId>pulsar-functions-api</artifactId>
    13. <version>3.0.1</version>
    14. </dependency>
    15. </dependencies>
    16. <build>
    17. <plugins>
    18. <plugin>
    19. <artifactId>maven-assembly-plugin</artifactId>
    20. <configuration>
    21. <appendAssemblyId>false</appendAssemblyId>
    22. <descriptorRefs>
    23. <descriptorRef>jar-with-dependencies</descriptorRef>
    24. </descriptorRefs>
    25. <archive>
    26. <manifest>
    27. <mainClass>org.example.test.ExclamationFunction</mainClass>
    28. </manifest>
    29. </archive>
    30. </configuration>
    31. <executions>
    32. <execution>
    33. <id>make-assembly</id>
    34. <phase>package</phase>
    35. <goals>
    36. <goal>assembly</goal>
    37. </goals>
    38. </execution>
    39. </executions>
    40. </plugin>
    41. <plugin>
    42. <groupId>org.apache.maven.plugins</groupId>
    43. <artifactId>maven-compiler-plugin</artifactId>
    44. <version>3.11.0</version>
    45. <configuration>
    46. <release>17</release>
    47. </configuration>
    48. </plugin>
    49. </plugins>
    50. </build>
    51. </project>
  2. Package your Java function.

    1. mvn package

    After the Java function is packaged, a target directory is created automatically. Open the target directory to check if there is a JAR package similar to java-function-1.0-SNAPSHOT.jar.

  3. Copy the packaged jar file to the Pulsar image.

    1. docker exec -it [CONTAINER ID] /bin/bash
    2. docker cp <path of java-function-1.0-SNAPSHOT.jar> CONTAINER ID:/pulsar
  4. Run the Java function using the following command.

    1. ./bin/pulsar-admin functions localrun \
    2. --classname org.example.test.ExclamationFunction \
    3. --jar java-function-1.0-SNAPSHOT.jar \
    4. --inputs persistent://public/default/my-topic-1 \
    5. --output persistent://public/default/test-1 \
    6. --tenant public \
    7. --namespace default \
    8. --name JavaFunction

    The following log indicates that the Java function starts successfully.

    1. ...
    2. 07:55:03.724 [main] INFO org.apache.pulsar.functions.runtime.ProcessRuntime - Started process successfully
    3. ...

Package as NAR

To package a Java function as NAR, complete the following steps.

  1. Create a new maven project with a pom file.

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>java-function</groupId>
    7. <artifactId>java-function</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.apache.pulsar</groupId>
    12. <artifactId>pulsar-functions-api</artifactId>
    13. <version>3.0.1</version>
    14. </dependency>
    15. </dependencies>
    16. <build>
    17. <plugins>
    18. <plugin>
    19. <groupId>org.apache.nifi</groupId>
    20. <artifactId>nifi-nar-maven-plugin</artifactId>
    21. <version>1.5.0</version>
    22. </plugin>
    23. <plugin>
    24. <groupId>org.apache.maven.plugins</groupId>
    25. <artifactId>maven-compiler-plugin</artifactId>
    26. <version>3.11.0</version>
    27. <configuration>
    28. <release>17</release>
    29. </configuration>
    30. </plugin>
    31. </plugins>
    32. </build>
    33. </project>

    You must also create a resources/META-INF/services/pulsar-io.yaml file. In the following code sample, the value of functionClass is your function class name. The name is the one used when the Function is deployed as a built-in one.

    1. name: java-function
    2. description: my java function
    3. functionClass: org.example.test.ExclamationFunction
  2. Package your Java function.

    1. mvn package

    After the Java function is packaged, a target directory is created automatically. Open the target directory to check if there is a NAR package similar to java-function-1.0-SNAPSHOT.nar.

  3. Copy the packaged NAR file to the Pulsar image.

    1. docker cp <path of java-function-1.0-SNAPSHOT.nar> CONTAINER ID:/pulsar
  4. Run the Java function using the following command.

    1. ./bin/pulsar-admin functions localrun \
    2. --jar java-function-1.0-SNAPSHOT.nar \
    3. --inputs persistent://public/default/my-topic-1 \
    4. --output persistent://public/default/test-1 \
    5. --tenant public \
    6. --namespace default \
    7. --name JavaFunction

    The following log indicates that the Java function starts successfully.

    1. ...
    2. 07:55:03.724 [main] INFO org.apache.pulsar.functions.runtime.ProcessRuntime - Started process successfully
    3. ...