Batch (Tumbling) Time

Provides examples on aggregating events over time in a batch (tumbling) manner.

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

@info(name = 'SensorId-analysis')
from TemperatureStream#window.timeBatch(30 sec, 0)
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.timeBatch(1 min)

Aggregate events every 1 minute, from the arrival of the first event.

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

Calculate average, maximum, and count for temperature attribute.

insert into OverallTemperatureStream;
@info(name = 'SensorId-analysis')
from TemperatureStream#window.timeBatch(30 sec, 0)

Aggregate events every 30 seconds from epoch timestamp 0.

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;

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:10 ['1001', 21.0] - -
9:00:20 ['1002', 25.0] - -
9:00:30 - - ['1001', 21.0, 21.0],['1002', 25.0, 25.0]
9:00:35 ['1002', 26.0] - -
9:00:40 ['1002', 27.0] - -
9:00:55 ['1001', 19.0] - -
9:00:00 - - ['1002', 26.5, 26.0]
9:01:10 - [23.6, 27.0, 5] -
9:01:20 ['1001', 21.0] - -
9:01:30 - - ['1001', 21.0, 21.0]
9:02:10 - [21.0, 21.0, 1] -
Top