Batch (Tumbling) Event Count

Provides examples on aggregating events based on event count in a batch (tumbling) manner.

To aggregate events in a sliding manner, 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.lengthBatch(4)
select avg(temperature) as avgTemperature,
       max(temperature) as maxTemperature,
       count() as numberOfEvents
insert into OverallTemperatureStream;

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

Aggregate every 4 events in a batch (tumbling) 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.lengthBatch(5)

Aggregate every 5 events in a batch (tumbling) 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] - -
['1002', 26.0] - -
['1002', 24.0] - -
['1001', 20.0] [22.5, 26.0, 4] -
['1001', 21.0] - ['1002', 25.5, 24.0],
['1001', 20.0, 19.0]
['1002', 22.0] - -
['1001', 21.0] - -
['1002', 22.0] [21.5, 22.0, 4] -
Top