Accessing state across Apache Pulsar functions

Viewed 302

I'd like to have one function aggregate events and maintain state in bookkeeper, while allowing other functions handling different events to leverage that state (either accessing by key, or ranging over keys to find the state.

I couldn't find any way to do it via the context object, is there a different way?

2 Answers

You can leverage the

/**
 * Update the state value for the key.
 *
 * @param key   name of the key
 * @param value state value of the key
 */
void putState(String key, ByteBuffer value);

and

/**
 * Retrieve the state value for the key.
 *
 * @param key name of the key
 * @return the state value for the key.
 */
ByteBuffer getState(String key);

methods of the Context Object to accomplish this as long as you coordinate the key you will use between the functions. The aggregate function would perform the calculation and store the data using the putState method and a pre-determined key like so:

public class AggregateFunction implements Function<String, Void> {
@Override
public Void process(String input, Context context) {
    ByteBuffer value;
    // Calculate value and place in ByteBuffer
    context.putState("PRE-DETERMINED-KEY", value);
}

}

the consuming functions can then access this value like so;

public class ConsumingFunction implements Function<String, String> {
@Override
public String process(String input, Context context) {
    ByteBuffer value = context.gettState("PRE-DETERMINED-KEY");
    // Perform logic based on the value.
    return "";
}

}

Related