A named window is a window that can be shared across multiple queries. Events can be inserted to a named window from one or more queries, and it can produce output events based on the named window type.
Windows provide a way to capture a subset of events from an input stream and retain them for a period of time based on a specified criterion. The criterion defines when and how the events should be evicted from the windows. Such as events being evicted from the window based on the time duration, or number of events and the events are evicted in a sliding (one by one) or tumbling (batch) manner.
When window is used as an input to a query, another window cannot be applied on top of it.
Windows help to retain events based on a criterion, such that the values of those events can be aggregated, or checked if an event of interest is within the window or not.
The syntax for a named window is as follows:
CREATE WINDOW <window name> (<attribute name> <attribute type>, <attribute name> <attribute type>, ... ) <window type>(<parameter>, <parameter>, …) <event type>;
The following parameters are configured in a named window definition:
|window name||The name of the window defined. (|
|attribute name||The schema of the window is defined by its attributes with uniquely identifiable attribute names (|
|attribute type||The type of each attribute defined in the schema. This can be |
|The window type associated with the window and its parameters. Refer to Window Types.|
|This is optional. Keywords such as |
Macrometa supports the following window types:
|CRON()||Retains events based on cron time in a tumbling (batch) manner.|
|DELAY()||Retains events and delays the output by the given time period in a sliding manner.|
|SESSION()||Retains events for each session based on session key.|
|SORT()||Retains top-k or bottom-k events based on a parameter value.|
|TUMBLING()||Retains events of last arrived event batch.|
|SLIDING_EXPRESSION()||Retains events based on an expression in a sliding manner.|
|TUMBLING_EXPRESSION()||Retains events based on an expression in a tumbling (batch) manner.|
|SLIDING_EXTERNAL_TIME()||Retains events based on event time value passed as a parameter in a sliding manner.|
|TUMBLING_EXTERNAL_TIME()||Retains events based on event time value passed as a parameter in a a tumbling (batch) manner.|
|SLIDING_LENGTH()||Retains events based on number of events in a sliding manner.|
|TUMBLING_LENGTH()||Retains events based on number of events in a tumbling (batch) manner.|
|SLIDING_TIME()||Retains events based on time in a sliding manner.|
|SLIDING_TIME_LENGTH()||Retains events based on time and number of events in a sliding manner.|
|TUMBLING_TIME()||Retains events based on time in a tumbling (batch) manner.|
CREATE WINDOW SensorWindow (name string, value float, roomNo int, deviceID string) TUMBLING_TIME(1 second);
This query returns all output when events arrive and when events expire from the window. The event type is not specified, so the stream worker returns both current and expired events as the output. This example uses the TUMBLING_TIME window.
CREATE WINDOW SensorWindow (name string, value float, roomNo int, deviceID string) TUMBLING_TIME(1 second) output expired events;
This query returns an output only when events expire from the window. The event type of the window is
expired events, so the stream worker only returns the events that have expired from the window as the output. This example uses the TUMBLING_TIME window.
This example shows a stream worker that defines a named window and summarizes data based on the window. This example uses the SLIDING_TIME window as the named window, but any window can be defined and used as a named window.
Stream Worker Code
CREATE STREAM TemperatureStream (sensorId string, temperature double);
CREATE SINK STREAM MinMaxTemperatureOver1MinStream(minTemperature double, maxTemperature double);
CREATE SINK STREAM AvgTemperaturePerSensorStream(sensorId string, avgTemperature double);
-- Define a named window with name `OneMinTimeWindow` to retain events over `1 minute` in a sliding manner.
CREATE WINDOW OneMinTimeWindow (sensorId string, temperature double) SLIDING_TIME(1 min) ;
@info(name = 'Insert-to-window')
-- Insert events in to the named time window.
INSERT INTO OneMinTimeWindow
@info(name = 'Min-max-analysis')
-- Calculate minimum and maximum of `temperature` on events in `OneMinTimeWindow` window.
INSERT INTO MinMaxTemperatureOver1MinStream
SELECT min(temperature) AS minTemperature,
max(temperature) AS maxTemperature
@info(name = 'Per-sensor-analysis')
-- Calculate average of `temperature`, by grouping events by `sensorId`, on the `OneMinTimeWindow` window.
INSERT INTO AvgTemperaturePerSensorStream
avg(temperature) AS avgTemperature
GROUP BY sensorId;
Named Window Aggregation Behavior
When events are sent to
TemperatureStream stream, the following events are emitted at
Min-max-analysis query, and
|Time||Input to ||Output at ||Output at |