2

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?

  • I've been looking for something similar, basically a way to use bookkeeper directly through Pulsar. But it seems it's not really possible (yet). If it's not a lot of state you could perhaps send it around with the message as part of it's properties. – Adriaan Jun 04 '20 at 10:40

2 Answers2

1

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 "";
}

}

David Kjerrumgaard
  • 1,056
  • 7
  • 10
  • I don't think this will work since the state is scoped to an individual function - from the Pulsar documentation: _States are key-value pairs, where the key is a string and the value is arbitrary binary data - counters are stored as 64-bit big-endian binary values. **Keys are scoped to an individual Pulsar Function, and shared between instances of that function**._ – Adriaan Jun 04 '20 at 10:36
0

As per documentation, use Pulsar Admin API for Functions 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.

If I get "ranging over keys" correctly, this discussion querying-cleaning state-without-key suggests it is not possible

Aiman
  • 1
  • 1