Manage Functions

Functions - 图1tip

This page only shows some frequently used operations. For the latest and complete information, see the reference docs below.

CategoryMethodIf you want to manage functions…
Pulsar CLIpulsar-admin, which lists all commands, flags, descriptions, and more.See the functions command
Pulsar admin APIsREST API, which lists all parameters, responses, samples, and more.See the /admin/v3/functions endpoint
Pulsar admin APIsJava admin API, which lists all classes, methods, descriptions, and more.See the functions method of the PulsarAdmin object

You can perform the following operations on functions.

Create a function

You can create a Pulsar function in cluster mode (deploy it on a Pulsar cluster) using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the create subcommand.

Example

  1. pulsar-admin functions create \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --inputs test-input-topic \
  6. --output persistent://public/default/test-output-topic \
  7. --classname org.apache.pulsar.functions.api.examples.ExclamationFunction \
  8. --jar $PWD/examples/api-examples.jar

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}

  1. FunctionConfig functionConfig = new FunctionConfig();
  2. functionConfig.setTenant(tenant);
  3. functionConfig.setNamespace(namespace);
  4. functionConfig.setName(functionName);
  5. functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
  6. functionConfig.setParallelism(1);
  7. functionConfig.setClassName("org.apache.pulsar.functions.api.examples.ExclamationFunction");
  8. functionConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATLEAST_ONCE);
  9. functionConfig.setTopicsPattern(sourceTopicPattern);
  10. functionConfig.setSubName(subscriptionName);
  11. functionConfig.setAutoAck(true);
  12. functionConfig.setOutput(sinkTopic);
  13. admin.functions().createFunction(functionConfig, fileName);

Update a function

You can update a Pulsar function that has been deployed to a Pulsar cluster using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the update subcommand.

Example

  1. pulsar-admin functions update \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --output persistent://public/default/update-output-topic \
  6. # other options

PUT /admin/v3/functions/{tenant}/{namespace}/{functionName}

  1. FunctionConfig functionConfig = new FunctionConfig();
  2. functionConfig.setTenant(tenant);
  3. functionConfig.setNamespace(namespace);
  4. functionConfig.setName(functionName);
  5. functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
  6. functionConfig.setParallelism(1);
  7. functionConfig.setClassName("org.apache.pulsar.functions.api.examples.ExclamationFunction");
  8. UpdateOptions updateOptions = new UpdateOptions();
  9. updateOptions.setUpdateAuthData(updateAuthData);
  10. admin.functions().updateFunction(functionConfig, userCodeFile, updateOptions);

Start a function

You can start an instance of a function or start all instances of a function.

Start an instance of a function

You can start a stopped function instance with instance-id using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the start subcommand.

  1. pulsar-admin functions start \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --instance-id 1

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/{instanceId}/start

  1. admin.functions().startFunction(tenant, namespace, functionName, Integer.parseInt(instanceId));

Start all instances of a function

You can start all stopped function instances using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the start subcommand.

Example

  1. pulsar-admin functions start \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/start

  1. admin.functions().startFunction(tenant, namespace, functionName);

Stop a function

You can stop an instance of a function or stop all instances of a function.

Stop an instance of a function

You can stop a function instance with instance-id using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the stop subcommand.

Example

  1. pulsar-admin functions stop \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --instance-id 1

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/{instanceId}/stop

  1. admin.functions().stopFunction(tenant, namespace, functionName, Integer.parseInt(instanceId));

Stop all instances of a function

You can stop all function instances using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the stop subcommand.

Example

  1. pulsar-admin functions stop \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions)

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/stop

  1. admin.functions().stopFunction(tenant, namespace, functionName);

Restart a function

You can restart an instance of a function or restart all instances of a function.

Restart an instance of a function

Restart a function instance with instance-id using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the restart subcommand.

Example

  1. pulsar-admin functions restart \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --instance-id 1

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/{instanceId}/restart

  1. admin.functions().restartFunction(tenant, namespace, functionName, Integer.parseInt(instanceId));

Restart all instances of a function

You can restart all function instances using Admin CLI, REST API or Java admin API.

  • Admin CLI
  • REST API
  • Java

Use the restart subcommand.

Example

  1. pulsar-admin functions restart \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions)

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/restart

  1. admin.functions().restartFunction(tenant, namespace, functionName);

List all functions

You can list all Pulsar functions running under a specific tenant and namespace using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the list subcommand.

Example

  1. pulsar-admin functions list \
  2. --tenant public \
  3. --namespace default

GET /admin/v3/functions/{tenant}/{namespace}

  1. admin.functions().getFunctions(tenant, namespace);

Delete a function

You can delete a Pulsar function that is running on a Pulsar cluster using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the delete subcommand.

Example

  1. pulsar-admin functions delete \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions)

DELETE /admin/v3/functions/{tenant}/{namespace}/{functionName}

  1. admin.functions().deleteFunction(tenant, namespace, functionName);

Get info about a function

You can get information about a Pulsar function currently running in cluster mode using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the get subcommand.

Example

  1. pulsar-admin functions get \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions)

GET /admin/v3/functions/{tenant}/{namespace}/{functionName}

  1. admin.functions().getFunction(tenant, namespace, functionName);

Get status of a function

You can get the status of an instance of a function or get the status of all instances of a function.

Get status of an instance of a function

You can get the current status of a Pulsar function instance with instance-id using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the status subcommand.

Example

  1. pulsar-admin functions status \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --instance-id 1

GET /admin/v3/functions/{tenant}/{namespace}/{functionName}/{instanceId}/status

  1. admin.functions().getFunctionStatus(tenant, namespace, functionName, Integer.parseInt(instanceId));

Get status of all instances of a function

You can get the current status of a Pulsar function instance using Admin CLI, REST API or Java Admin API.

  • Admin CLI
  • REST API
  • Java

Use the status subcommand.

Example

  1. pulsar-admin functions status \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions)

GET /admin/v3/functions/{tenant}/{namespace}/{functionName}/status

  1. admin.functions().getFunctionStatus(tenant, namespace, functionName);

Get stats of a function

You can get stats of an instance of a function or get stats of all instances of a function.

Get stats of an instance of a function

You can get the current stats of a Pulsar Function instance with instance-id using Admin CLI, REST API or Java admin API.

  • Admin CLI
  • REST API
  • Java

Use the stats subcommand.

Example

  1. pulsar-admin functions stats \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --instance-id 1

GET /admin/v3/functions/{tenant}/{namespace}/{functionName}/{instanceId}/stats

  1. admin.functions().getFunctionStats(tenant, namespace, functionName, Integer.parseInt(instanceId));

Get stats of all instances of a function

You can get the current stats of a Pulsar function using Admin CLI, REST API or Java admin API.

  • Admin CLI
  • REST API
  • Java

Use the stats subcommand.

Example

  1. pulsar-admin functions stats \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions)

GET /admin/v3/functions/{tenant}/{namespace}/{functionName}/stats

  1. admin.functions().getFunctionStats(tenant, namespace, functionName);

Trigger a function

You can trigger a specified Pulsar function with a supplied value using Admin CLI, REST API or Java admin API.

  • Admin CLI
  • REST API
  • Java

Use the trigger subcommand.

Example

  1. pulsar-admin functions trigger \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --topic (the name of input topic) \
  6. --trigger-value \"hello pulsar\"
  7. # or --trigger-file (the path of trigger file)

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/trigger

  1. admin.functions().triggerFunction(tenant, namespace, functionName, topic, triggerValue, triggerFile);

Put state associated with a function

You can put the state associated with a Pulsar function using Admin CLI, REST API or Java admin API.

  • Admin CLI
  • REST API
  • Java

Use the putstate subcommand.

Example

  1. pulsar-admin functions putstate \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --state "{\"key\":\"pulsar\", \"stringValue\":\"hello pulsar\"}"

POST /admin/v3/functions/{tenant}/{namespace}/{functionName}/state/{key}

  1. TypeReference<FunctionState> typeRef = new TypeReference<FunctionState>() {};
  2. FunctionState stateRepr = ObjectMapperFactory.getThreadLocal().readValue(state, typeRef);
  3. admin.functions().putFunctionState(tenant, namespace, functionName, stateRepr);

Fetch state associated with a function

You can fetch the current state associated with a Pulsar function using Admin CLI, REST API or Java admin API.

  • Admin CLI
  • REST API
  • Java

Use the querystate subcommand.

Example

  1. pulsar-admin functions querystate \
  2. --tenant public \
  3. --namespace default \
  4. --name (the name of Pulsar Functions) \
  5. --key (the key of state)

GET /admin/v3/functions/{tenant}/{namespace}/{functionName}/state/{key}

  1. admin.functions().getFunctionState(tenant, namespace, functionName, key);