Defining size-based windows
As well as time, you can specify windows that contain only a certain number of items. In a size-based window, as each new item arrives, it is added to the window. After the number of items in the window reaches the window size limit specified in the query, the arrival of a new item causes the removal of the oldest item from the window.
The syntax for defining a size-based window is as follows:
retain windowSizeExpr
Replace windowSizeExpr with an expression that returns how many items you want to retain in the window as an integer value. For example, the following query calculates the sum of the last 2 items in a stream of floats:
stream <float> sums := from v in values retain 2 select sum(v.number);
The following diagram, which uses the same notation as the previous section, illustrates how this works in practice.
The query before the diagram corresponds to the aggregate projection. The three queries shown here are:
Simple istream Projection | from v in values retain 2 select v.number |
Simple rstream Projection | from v in values retain 2 select rstream v.number |
Aggregate Projection | from v in values retain 2 select sum(v.number) |
When an event arrives in the window it appears in the istream of a simple, non-aggregate projection. The first item remains in the window when a second item arrives. When a third item arrives, the first item is no longer in the window and it appears on the rstream of the simple, non-aggregate projection. Likewise, when the fourth item arrives in the window it appears in the istream and the second item appears on the rstream of the simple projection, and so on. The behavior of the aggregate projection is that whenever an item arrives in or is removed from the window, a new sum appears on the istream of the aggregate projection.
Copyright © 2013
Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.