Saturday, December 7, 2024

Types of Azure Stream Analytics windowing functions - Tumbling, Hopping, Sliding, Session and Snapshot window

Examples of each type of window in Azure Stream Analytics:

Tumbling Window

A tumbling window is a series of non-overlapping, fixed-sized, contiguous time intervals. For example, you can count the number of events in each 10-second interval:

sql
SELECT 
    System.Timestamp() AS WindowEnd, 
    TollId, 
    COUNT(*) 
FROM 
    Input 
TIMESTAMP BY 
    EntryTime 
GROUP BY 
    TollId, 
    TumblingWindow(second, 10)

Hopping Window

A hopping window is similar to a tumbling window but allows overlapping intervals. For example, you can count the number of events every 5 seconds within a 10-second window:

sql
SELECT 
    System.Timestamp() AS WindowEnd, 
    TollId, 
    COUNT(*) 
FROM 
    Input 
TIMESTAMP BY 
    EntryTime 
GROUP BY 
    TollId, 
    HoppingWindow(second, 10, 5)

Sliding Window

A sliding window moves forward by a specified interval and includes all events within that window. For example, you can calculate the average temperature over the last 30 seconds, updated every 5 seconds:

sql
SELECT 
    System.Timestamp() AS WindowEnd, 
    AVG(Temperature) 
FROM 
    Input 
TIMESTAMP BY 
    EntryTime 
GROUP BY 
    SlidingWindow(second, 30, 5)

Session Window

A session window groups events that are close in time, based on a specified gap duration. For example, you can count the number of events in sessions where events are no more than 30 seconds apart:

sql
SELECT 
    System.Timestamp() AS WindowEnd, 
    COUNT(*) 
FROM 
    Input 
TIMESTAMP BY 
    EntryTime 
GROUP BY 
    SessionWindow(second, 30)

Snapshot Window

A snapshot window captures the state of the stream at a specific point in time. For example, you can take a snapshot of the current state of a stream every minute:

sql
SELECT 
    System.Timestamp() AS SnapshotTime, 
    COUNT(*) 
FROM 
    Input 
TIMESTAMP BY 
    EntryTime 
GROUP BY 
    SnapshotWindow(second, 60)