Skip to main content
Version: 3.3.x

Pass user-defined configurations

When you run or update functions created via SDK, you can pass arbitrary key/value pairs to them by using CLI with the --user-config flag. Key/value pairs must be specified as JSON.

note

For all key/value pairs passed to Java functions, both keys and values are string. To set the value to be a different type, you need to deserialize it from the string type.

The context object of Java SDK enables you to access key/value pairs provided to Pulsar Functions via CLI (as JSON). The following example passes a key/value pair.

bin/pulsar-admin functions create \
# Other function configs
--user-config '{"word-of-the-day":"verdure"}'

To access that value in a Java function:

import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;
import org.slf4j.Logger;

import java.util.Optional;

public class UserConfigFunction implements Function<String, Void> {
@Override
public void apply(String input, Context context) {
Logger LOG = context.getLogger();
Optional<String> wotd = context.getUserConfigValue("word-of-the-day");
if (wotd.isPresent()) {
LOG.info("The word of the day is {}", wotd);
} else {
LOG.warn("No word of the day provided");
}
return null;
}
}

The UserConfigFunction function logs the string "The word of the day is verdure" every time the function is invoked. The word-of-the-day config can be changed only when the function is updated with a new value via the CLI.

You can also access the entire user config map or set a default value in case no value is present.

// Get the whole config map
Map<String, String> allConfigs = context.getUserConfigMap();

// Get value or resort to default
String wotd = context.getUserConfigValueOrDefault("word-of-the-day", "perspicacious");