Named Window

Provides examples on defining a named window, and summarizing data based on that. This example uses time window as the named window, but any window can be defined and used as a name window.

For more information on named windows refer the Siddhi query guide.

define stream TemperatureStream
        (sensorId string, temperature double);

define window OneMinTimeWindow
        (sensorId string, temperature double) time(1 min) ;

@info(name = 'Insert-to-window')
from TemperatureStream
insert into OneMinTimeWindow;

@info(name = 'Min-max-analysis')
from OneMinTimeWindow
select min(temperature) as minTemperature,
       max(temperature) as maxTemperature
insert into MinMaxTemperatureOver1MinStream;

@info(name = 'Per-sensor-analysis')
from OneMinTimeWindow
select sensorId,
       avg(temperature) as avgTemperature
group by sensorId
insert into AvgTemperaturePerSensorStream;
define stream TemperatureStream
        (sensorId string, temperature double);
define window OneMinTimeWindow
        (sensorId string, temperature double) time(1 min) ;

Define a named window with name OneMinTimeWindow to retain events over 1 minute in a sliding manner.

@info(name = 'Insert-to-window')
from TemperatureStream
insert into OneMinTimeWindow;

Insert events in to the named time window.

@info(name = 'Min-max-analysis')
from OneMinTimeWindow
select min(temperature) as minTemperature,
       max(temperature) as maxTemperature

Calculate minimum and maximum of temperature on events in OneMinTimeWindow window.

insert into MinMaxTemperatureOver1MinStream;
@info(name = 'Per-sensor-analysis')
from OneMinTimeWindow
select sensorId,
       avg(temperature) as avgTemperature
group by sensorId

Calculate average of temperature, by grouping events by sensorId, on the OneMinTimeWindow window.

insert into AvgTemperaturePerSensorStream;

Aggregation Behavior

When events are sent to TemperatureStream stream, following events will get emitted at MinMaxTemperatureOver1MinStream stream via Min-max-analysis query, and AvgTemperaturePerSensorStream stream via Per-sensor-analysis query.

Time Input to TemperatureStream Output at MinMaxTemperatureOver1MinStream Output at AvgTemperaturePerSensorStream
9:00:10 ['1001', 21.0] [21.0, 21.0] ['1001', 21.0]
9:00:20 ['1002', 25.0] [21.0, 25.0] ['1002', 25.0]
9:00:35 ['1002', 26.0] [21.0, 26.0] ['1002', 25.5]
9:00:40 ['1002', 27.0] [21.0, 27.0] ['1002', 26.0]
9:00:55 ['1001', 19.0] [19.0, 27.0] ['1001', 20.0]
9:01:30 ['1002', 22.0] [19.0, 27.0] ['1002', 25.0]
9:02:10 ['1001', 18.0] [18.0, 22.0] ['1001', 18.0]
Top