Using partitions and groups in stream queries
The second code example in
Using joins in stream queries uses the
partition by clause. The
partition by clause splits a stream into partitions, based on a key value. When a window operator is applied to a partitioned stream, the behavior is as if a separate window operator had been applied to each partition. We often refer to the result of using
partition by followed by a window operator as a partitioned window; queries with partitioned windows are used to retain a set of items for each partition, as illustrated in the second code example in topic about using joins. Following is another example of using the
partition by clause:
01. Temperature temperature;
02. from t in all Temperature() partition by t.sensorId retain 3
03. group by t.sensorId select Temperature(t.sensorId, mean(t.temperature)):
04. temperature {
05. print temperature.toString();
06. }
The combined partition by and retain clauses cause the last three values for each sensor to be retained. In contrast, the group by clause's effect is to alter the behavior of the projection (the item generated by the select clause) such that aggregate values are generated for each group in the collection and not for the collection as a whole. For example, when a new Temperature event occurs for sensor "S001", the event will be directed to the partition for that sensor. It will cause the window contents for that partition to change, which, in turn, will affect the collection of events over which the aggregate projection is being performed. Because a group by clause is present, a new projected value will be produced only for the group(s) affected by the update. In this case, the group for sensorId "S001" . The result is that an incoming temperature event, for sensor "S001", causes a new outgoing mean temperature event for sensor "S001" to be produced. The group by clause can also be used without partition by, as in the following code sample.11
01. Temperature temperature;
02. from t in all Temperature() within 60.0
03. group by t.sensorId select Temperature(t.sensorId, mean(t.temperature)):
04. temperature {
05. print temperature.toString();
06. }
11 As implied by the example, there is usually little point in partitioning a time-based (a within) window. One exception to this is when it is combined with the with unique clause.