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.

  • Java
  • Python
  • Go

Pass user-defined configurations - 图1note

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.

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

To access that value in a Java function:

  1. import org.apache.pulsar.functions.api.Context;
  2. import org.apache.pulsar.functions.api.Function;
  3. import org.slf4j.Logger;
  4. import java.util.Optional;
  5. public class UserConfigFunction implements Function<String, Void> {
  6. @Override
  7. public void apply(String input, Context context) {
  8. Logger LOG = context.getLogger();
  9. Optional<String> wotd = context.getUserConfigValue("word-of-the-day");
  10. if (wotd.isPresent()) {
  11. LOG.info("The word of the day is {}", wotd);
  12. } else {
  13. LOG.warn("No word of the day provided");
  14. }
  15. return null;
  16. }
  17. }

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.

  1. // Get the whole config map
  2. Map<String, String> allConfigs = context.getUserConfigMap();
  3. // Get value or resort to default
  4. String wotd = context.getUserConfigValueOrDefault("word-of-the-day", "perspicacious");

In a Python function, you can access the configuration value like this.

  1. from pulsar import Function
  2. class WordFilter(Function):
  3. def process(self, context, input):
  4. forbidden_word = context.user_config()["forbidden-word"]
  5. # Don't publish the message if it contains the user-supplied
  6. # forbidden word
  7. if forbidden_word in input:
  8. pass
  9. # Otherwise publish the message
  10. else:
  11. return input

The context object of Python SDK enables you to access key/value pairs provided to functions via the command line (as JSON). The following example passes a key/value pair.

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

To access that value in a Python function:

  1. from pulsar import Function
  2. class UserConfigFunction(Function):
  3. def process(self, input, context):
  4. logger = context.get_logger()
  5. wotd = context.get_user_config_value('word-of-the-day')
  6. if wotd is None:
  7. logger.warn('No word of the day provided')
  8. else:
  9. logger.info("The word of the day is {0}".format(wotd))

The context object of Go SDK enables you to access key/value pairs provided to functions via the command line (as JSON). The following example passes a key/value pair.

  1. bin/pulsar-admin functions create \
  2. --go path/to/go/binary
  3. --user-config '{"word-of-the-day":"lackadaisical"}'

To access that value in a Go function:

  1. func contextFunc(ctx context.Context) {
  2. fc, ok := pf.FromContext(ctx)
  3. if !ok {
  4. logutil.Fatal("Function context is not defined")
  5. }
  6. wotd := fc.GetUserConfValue("word-of-the-day")
  7. if wotd == nil {
  8. logutil.Warn("The word of the day is empty")
  9. } else {
  10. logutil.Infof("The word of the day is %s", wotd.(string))
  11. }
  12. }