Tumbling Window Examples (Cache Queries)

A tumbling window a specified queue size, specified as a certain number of entities, and empties each time the maximum size is exceeded. Emptying the window completes one cycle. The lifetime of an entity in the window, therefore, is one cycle.

The following query maintains a count over a tumbling window of events. Every time events arrive, the query picks up a maximum of 500 events, passes them through the query processing stages, in this case a counter, and produces the count as the result. Because this is a tumbling window, all those 500 or less events expire immediately and so the query runs once again and flushes all the events from the window. Now, the count drops to 0 and the query produces "0" as the count.

Tip: The following example uses a constant (in this case group by "") to create a dummy group. The next example uses a different constant. See Using a Dummy Group Expression for Aggregation for more details.
select count(*) from /BurgerSoldEvent {policy: maintain last 500 tumbling} burger group by "";

The following query is not very useful because it forgets how many events have been processed every time the window "tumbles." One way to solve this problem is to store all the events in a very large window, forever—but this is impractical. Another way is shown next.

select count(*) from /BurgerSoldEvent {policy: maintain last 500 tumbling; emit: new} burger group by 1;

Using Emit New to Create a Counter

You can define a tumbling window which retains events for just one cycle and then keep a counter that remains pinned even if the window appears to disappear after it empties itself.

To create such a counter, use the emit: new clause. This clause indicates to the query that it should only record events entering the window and not those exiting it. So, in this case the count keeps increasing as new events arrive and it never decreases.