Defining time-based windows
In a time-based window, the items are held in the window for a specific duration. The syntax for defining a time-based window is:
within windowDurationExpr
Replace windowDurationExpr with an expression that returns the number of seconds that items should remain in the window as a float value. For example, the following query calculates the sum of all items that arrived in a stream of float values during the last 1.5 seconds:
stream<float> sums := from v in values within 1.5 select sum(v);
The following diagram illustrates how this works in practice.
Each column represents a time when the query window contents change whereas each row represents the arrival and lifetime of each event. As an event arrives in the window it appears in bold purple. At each given time, the current window contents is indicated by the items enclosed by boxes — bold purple items are new and lighter purple items are old items still in the window.The numbers at the bottom give the contents of the stream of insertions to and removals from the window in the case where each value is being selected independently, or when the aggregate sum of the values in the set of items in the window is being calculated. The query before the diagram corresponds to the aggregate projection line. The queries shown here are:
Simple istream Projection | from v in values within 1.5 select v |
Simple rstream Projection | from v in values within 1.5 select rstream v |
Aggregate Projection | from v in values within 1.5 select sum(v) |
In a simple, non-aggregate projection, when an event arrives in the window it appears in the istream of the projection. It remains for 1.5 seconds, at which point it appears on the rstream of the projection. The aggregate projection behaves differently. Whenever an item arrives in or is removed from the window, a new sum appears on the istream of the aggregate projection.