Sliding Time

Provides examples on aggregating events over time in a sliding manner.

To aggregate events in batches, based on events, or by session, refer other the examples in Data Summarization section. For more information on windows refer the Siddhi query guide.

define stream TemperatureStream
        (sensorId string, temperature double);

@info(name = 'Overall-analysis')
from TemperatureStream#window.time(1 min)
select avg(temperature) as avgTemperature,
       max(temperature) as maxTemperature,
       count() as numberOfEvents
insert all events into OverallTemperatureStream;

@info(name = 'SensorId-analysis')
from TemperatureStream#window.time(30 sec)
select sensorId,
       avg(temperature) as avgTemperature,
       min(temperature) as maxTemperature
group by sensorId
having avgTemperature > 20.0
insert into SensorIdTemperatureStream;
define stream TemperatureStream
        (sensorId string, temperature double);
@info(name = 'Overall-analysis')
from TemperatureStream#window.time(1 min)

Aggregate events over 1 minute sliding window

select avg(temperature) as avgTemperature,
       max(temperature) as maxTemperature,
       count() as numberOfEvents

Calculate average, maximum, and count for temperature attribute.

insert all events into OverallTemperatureStream;

Output when events are added, and removed (expired) from window.time().

@info(name = 'SensorId-analysis')
from TemperatureStream#window.time(30 sec)

Aggregate events over 30 seconds sliding window

select sensorId,
       avg(temperature) as avgTemperature,
       min(temperature) as maxTemperature
group by sensorId

Calculate average, and minimum for temperature, by grouping events by sensorId.

having avgTemperature > 20.0

Output events only when avgTemperature is greater than 20.0.

insert into SensorIdTemperatureStream;

Output only when events are added to window.time().

Aggregation Behavior

When events are sent to TemperatureStream stream, following events will get emitted at OverallTemperatureStream stream via Overall-analysis query, and SensorIdTemperatureStream stream via SensorId-analysis query.

Time Input to TemperatureStream Output at OverallTemperatureStream Output at SensorIdTemperatureStream
9:00:00 ['1001', 18.0] [18.0, 18.0, 1] No events, as having
condition not satisfied.
9:00:10 ['1002', 23.0] [20.5, 23.0, 2] ['1002', 23.0, 23.0]
9:00:20 ['1002', 22.0] [21.0, 23.0, 3] ['1002', 22.5, 22.0]
9:00:40 - - No events, as expired
events are not emitted.
9:00:50 - - No events, as expired
events are not emitted.
9:00:00 - [22.5, 23.0, 2] -
9:01:10 ['1001', 17.0] [19.5, 22.0, 2] -
9:01:20 - [17.0, 17.0, 1] -
9:02:10 - [null, null, 0] -
Top