Package org.voltdb.stream.function
Class BatchAccumulator<I,O>
java.lang.Object
org.voltdb.stream.function.BatchAccumulator<I,O>
- Type Parameters:
I
- the type of input elements to be accumulatedO
- the type of the accumulated result
- All Implemented Interfaces:
VoltFunction<I,
,O> VoltLifecycle
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 Summary
ConstructorDescriptionBatchAccumulator
(O accumulator, BiFunction<O, I, O> accumulatorFunction) Constructs aBatchAccumulator
with the specified initial accumulator and accumulation function. -
Method Summary
Modifier and TypeMethodDescriptionvoid
batchProcessed
(long batchId) the callback is invoked when the function finishes processing a batch of datavoid
initialize
(Consumer<O> consumer) 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.void
process
(I input, Consumer<O> consumer, ExecutionContext context) processes input and emits output messages to a consumerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.voltdb.stream.api.pipeline.VoltFunction
nextBatchStarts
Methods inherited from interface org.voltdb.stream.api.pipeline.VoltLifecycle
configure, destroy
-
Constructor Details
-
BatchAccumulator
Constructs aBatchAccumulator
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 accumulatoraccumulatorFunction
- aBiFunction
that combines the current accumulator with an input item
-
-
Method Details
-
initialize
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 interfaceVoltFunction<I,
O> - Parameters:
consumer
- a downstream consumer used to emit transformed events to it
-
process
Description copied from interface:VoltFunction
processes input and emits output messages to a consumer- Specified by:
process
in interfaceVoltFunction<I,
O> - Parameters:
input
- messageconsumer
- binds this function with next stepcontext
- 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 interfaceVoltFunction<I,
O> - Parameters:
batchId
- globally unique id to track messages related to current batch of data
-