eventscomplex-event-processingesper

Esper sum of snapshots (as opposed to sum of deltas)


Esper sum is great for adding up deltas. For example,

select orderId, sum(quantity) from OrderEvent

with three OrderEvents that come in,

OrderEvent(orderId=1 quantity=10)
OrderEvent(orderId=3, quantity=10)
OrderEvent (orderId=1 quantity=5)

as listed above, will summarized the quantity as 25.

But notice that the second row is actually an update on orderId==1.

Is there a way in Esper to take the last "snapshot" and sum up those quantities.

So for the example above, the first row would be replaced with the third when calculating the snapshot sum, and result would be 15 and not 25.

Resulting in the following sequence for sum: 10, 20, 15.


Solution

  • For incremental aggregations of subsets of events there are data windows. Use the #unique data window for the subset of events that is the last unique event per key value(s).

    select orderId, sum(quantity) from OrderEvent#unique(orderId)