Chapter 8 Working With Continuous Queries : Tumbling Window Examples

Tumbling Window Examples
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.

 
select count(*) from /BurgerSoldEvent {policy: maintain latest 500} burger group by 1;

 
The above 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.

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

 
The query above 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 to 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.
emit: new clause
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.