Sliding Event Count

Provides examples on aggregating events based on event count in a sliding manner.

To aggregate events in batches, based on time, 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.length(4)
select avg(temperature) as avgTemperature,
       max(temperature) as maxTemperature,
       count() as numberOfEvents
insert into OverallTemperatureStream;

@info(name = 'SensorId-analysis')
from TemperatureStream#window.length(5)
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.length(4)

Aggregate last 4 events in a sliding manner.

select avg(temperature) as avgTemperature,
       max(temperature) as maxTemperature,
       count() as numberOfEvents
insert into OverallTemperatureStream;

Calculate average, maximum, and count for temperature attribute.

@info(name = 'SensorId-analysis')
from TemperatureStream#window.length(5)

Aggregate last 5 events in a sliding manner.

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 or equal to 20.0.

insert into SensorIdTemperatureStream;

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.

Input to TemperatureStream Output at OverallTemperatureStream Output at SensorIdTemperatureStream
['1001', 19.0] [19.0, 19.0, 1] No events, as having
condition not satisfied
for '1001'.
['1002', 26.0] [22.5, 26.0, 2] ['1002', 26.0, 26.0]
['1002', 24.0] [23.0, 26.0, 3] ['1002', 25.5, 24.0]
['1001', 20.0] [22.5, 26.0, 4] No events, as having
condition not satisfied
for '1001'.
['1001', 21.0] [22.75, 26.0, 4] ['1001', 20.0, 19.0]
['1001', 22.0] [21.75, 24.0, 4] ['1001', 21.0, 20.0]
Top