Use APIs

The following table outlines the APIs that you can use to develop Pulsar Functions in Java, Python, and Go.

InterfaceDescriptionUse case
Language-native interface for Java/PythonNo Pulsar-specific libraries or special dependencies required (only core libraries).Functions that do not require access to the context.
Pulsar Functions SDK for Java/Python/GoPulsar-specific libraries that provide a range of functionality not available in the language-native interfaces, such as state management or user configuration.Functions that require access to the context.
Extended Pulsar Functions SDK for JavaAn extension to Pulsar-specific libraries, providing the initialization and close interfaces in Java.Functions that require initializing and releasing external resources.

Use language-native interface for Java/Python

The language-native interface provides a simple and clean approach to writing Java/Python functions, by adding an exclamation point to all incoming strings and publishing the output string to a topic. It has no external dependencies.

The following examples are language-native functions.

  • Java
  • Python

To use a piece of Java code as a “language-native” function, you need to implement the java.util.Function interface. You can include any sort of complex logic inside the apply method to provide more processing capabilities.

  1. import java.util.function.Function;
  2. public class JavaNativeExclamationFunction implements Function<String, String> {
  3. @Override
  4. public String apply(String input) {
  5. return String.format("%s!", input);
  6. }
  7. }

For more details, see code example.

To use a piece of Python code as a “language-native” function, you must have a method named process as follows. It appends an exclamation point to any string value it receives.

  1. def process(input):
  2. return "{}!".format(input)

For more details, see code example.

Use APIs - 图1note

Write Pulsar Functions in Python 3. To make sure your functions can run, you need to have Python 3 installed for functions workers and set Python 3 as the default interpreter.

Use SDK for Java/Python/Go

The implementation of Pulsar Functions SDK specifies a functional interface that includes the context object as a parameter.

The following examples use Pulsar Functions SDK for different languages.

  • Java
  • Python
  • Go

When developing a function using the Java SDK, you need to implement the org.apache.pulsar.functions.api.Function interface. It specifies only one method that you need to implement called process.

  1. import org.apache.pulsar.functions.api.Context;
  2. import org.apache.pulsar.functions.api.Function;
  3. public class ExclamationFunction implements Function<String, String> {
  4. @Override
  5. public String process(String input, Context context) {
  6. return String.format("%s!", input);
  7. }
  8. }

For more details, see code example.

The return type of the function can be wrapped in a Record generic which gives you more control over the output messages, such as topics, schemas, properties, and so on. Use the Context::newOutputRecordBuilder method to build this Record output.

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import org.apache.pulsar.functions.api.Context;
  4. import org.apache.pulsar.functions.api.Function;
  5. import org.apache.pulsar.functions.api.Record;
  6. public class RecordFunction implements Function<String, Record<String>> {
  7. @Override
  8. public Record<String> process(String input, Context context) throws Exception {
  9. String output = String.format("%s!", input);
  10. Map<String, String> properties = new HashMap<>(context.getCurrentRecord().getProperties());
  11. context.getCurrentRecord().getTopicName().ifPresent(topic -> properties.put("input_topic", topic));
  12. return context.newOutputRecordBuilder(Schema.STRING)
  13. .value(output)
  14. .properties(properties)
  15. .build();
  16. }
  17. }

For more details, see code example.

To develop a function using the Python SDK, you need to add the pulsar client dependency to your Python installation.

  1. from pulsar import Function
  2. class ExclamationFunction(Function):
  3. def __init__(self):
  4. pass
  5. def process(self, input, context):
  6. return input + '!'

For more details, see code example.

To develop a function using the Go SDK, you need to add the pulsar client dependency to your Go installation and provide the name of the function to the pf.Start() method inside the main() method. This registers the function with the Pulsar Functions framework and ensures that the specified function can be invoked when a new message arrives.

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/apache/pulsar/pulsar-function-go/pf"
  6. )
  7. func HandleRequest(ctx context.Context, in []byte) error{
  8. fmt.Println(string(in) + "!")
  9. return nil
  10. }
  11. func main() {
  12. pf.Start(HandleRequest)
  13. }

For more details, see code example.

Use extended SDK for Java

This extended Pulsar Functions SDK provides two additional interfaces to initialize and release external resources.

  • By using the initialize interface, you can initialize external resources which only need one-time initialization when the function instance starts.
  • By using the close interface, you can close the referenced external resources when the function instance closes.

Use APIs - 图2note

The extended Pulsar Functions SDK for Java is only available in Pulsar 2.10.0 or later versions. Before using it, you need to set up function workers in Pulsar 2.10.0 or later versions.

The following example uses the extended interface of Pulsar Functions SDK for Java to initialize RedisClient when the function instance starts and release it when the function instance closes.

  • Java
  1. import org.apache.pulsar.functions.api.Context;
  2. import org.apache.pulsar.functions.api.Function;
  3. import io.lettuce.core.RedisClient;
  4. public class InitializableFunction implements Function<String, String> {
  5. private RedisClient redisClient;
  6. private void initRedisClient(Map<String, Object> connectInfo) {
  7. redisClient = RedisClient.create(connectInfo.get("redisURI"));
  8. }
  9. @Override
  10. public void initialize(Context context) {
  11. Map<String, Object> connectInfo = context.getUserConfigMap();
  12. redisClient = initRedisClient(connectInfo);
  13. }
  14. @Override
  15. public String process(String input, Context context) {
  16. String value = client.get(key);
  17. return String.format("%s-%s", input, value);
  18. }
  19. @Override
  20. public void close() {
  21. redisClient.close();
  22. }
  23. }