Class BatchAccumulator<I,O>

java.lang.Object
org.voltdb.stream.function.BatchAccumulator<I,O>
Type Parameters:
I - the type of input elements to be accumulated
O - the type of the accumulated result
All Implemented Interfaces:
VoltFunction<I,O>, VoltLifecycle

public class BatchAccumulator<I,O> extends Object implements VoltFunction<I,O>
A processing function that accumulates incoming data items into a single aggregated result and emits the result once a batch is processed.

It uses a provided BiFunction to accumulate each input item into an accumulator of type O. After processing a batch of inputs, the accumulated result is emitted through the provided Consumer.

Usage Example


 // Creating an BatchAccumulator instance
 var batchAccumulator = new BatchAccumulator<Integer, Long>(0L, Long::sum);

 // Use it as a function in a pipeline.
 stream
   .withName("simple stream")
   .consumeFromSource(Sources.collection("A", "AB", "CDE"))
   .processWith(input -> input.getBytes(StandardCharsets.UTF_8).length)
   .processWith(batchAccumulator)
   .terminateWithSink(Sinks.consume(output -> log.info("bytes consumed by the batch is {}", output)));
 
  • Constructor Details

    • BatchAccumulator

      public BatchAccumulator(O accumulator, BiFunction<O,I,O> accumulatorFunction)
      Constructs a BatchAccumulator with the specified initial accumulator and accumulation function.

      The accumulator function defines how each input item is combined with the current accumulator to produce a new accumulator value.

      Parameters:
      accumulator - the initial value of the accumulator
      accumulatorFunction - a BiFunction that combines the current accumulator with an input item
  • Method Details

    • initialize

      public void initialize(Consumer<O> consumer)
      Description copied from interface: VoltFunction
      the callback is invoked when the function is created and a consumer is assigned but the function is not yet scheduled to handle any incoming events.
      Specified by:
      initialize in interface VoltFunction<I,O>
      Parameters:
      consumer - a downstream consumer used to emit transformed events to it
    • process

      public void process(I input, Consumer<O> consumer, ExecutionContext context)
      Description copied from interface: VoltFunction
      processes input and emits output messages to a consumer
      Specified by:
      process in interface VoltFunction<I,O>
      Parameters:
      input - message
      consumer - binds this function with next step
      context - of the execution
    • batchProcessed

      public void batchProcessed(long batchId)
      Description copied from interface: VoltFunction
      the callback is invoked when the function finishes processing a batch of data
      Specified by:
      batchProcessed in interface VoltFunction<I,O>
      Parameters:
      batchId - globally unique id to track messages related to current batch of data