Debug with localrun mode

In localrun mode, a function consumes and produces actual data to a Pulsar cluster, and mirrors how the function runs in a Pulsar cluster. This provides a way to test your function and allows you to launch a function instance on your local machine as a thread for easy debugging.

Debug with localrun mode - 图1note

Debugging with localrun mode is only available for Java functions in Pulsar 2.4.0 or later versions.

Before using localrun mode, you need to add the following dependency.

  1. <dependency>
  2. <groupId>org.apache.pulsar</groupId>
  3. <artifactId>pulsar-functions-local-runner-original</artifactId>
  4. <version>${pulsar.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.google.protobuf</groupId>
  8. <artifactId>protobuf-java</artifactId>
  9. <version>3.21.9</version>
  10. </dependency>

For example, you can run your function in the following manner.

  1. FunctionConfig functionConfig = new FunctionConfig();
  2. functionConfig.setName(functionName);
  3. functionConfig.setInputs(Collections.singleton(sourceTopic));
  4. functionConfig.setClassName(ExclamationFunction.class.getName());
  5. functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
  6. functionConfig.setOutput(sinkTopic);
  7. LocalRunner localRunner = LocalRunner.builder().functionConfig(functionConfig).build();
  8. localRunner.start(true);

You can debug functions using an IDE. Set breakpoints and manually step through a function to debug with real data.

The following code example shows how to run a function in localrun mode.

  1. public class ExclamationFunction implements Function<String, String> {
  2. @Override
  3. public String process(String s, Context context) throws Exception {
  4. return s + "!";
  5. }
  6. public static void main(String[] args) throws Exception {
  7. FunctionConfig functionConfig = new FunctionConfig();
  8. functionConfig.setName("exclamation");
  9. functionConfig.setInputs(Collections.singleton("input"));
  10. functionConfig.setClassName(ExclamationFunction.class.getName());
  11. functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
  12. functionConfig.setOutput("output");
  13. LocalRunner localRunner = LocalRunner.builder().functionConfig(functionConfig).build();
  14. localRunner.start(false);
  15. }
  16. }