This topic describes the window functions, time attributes, and window types that Flink SQL supports.
Flink SQL supports aggregation over infinite windows. You do not need to explicitly add any windows in your SQL query statement. In addition, Flink SQL supports aggregation over a specific window. For example, to count the number of users who clicked a URL in the last minute, you can define a window to collect user clicks in the last minute. Then, you can compute the data in the window to obtain the result.
Flink SQL supports window aggregate and over aggregate. This topic describes window aggregate. Window aggregate supports two time attributes: event time and processing time. For each time attribute, Flink SQL supports three window types: tumbling window, sliding window, and session window.
- Event time: The event time that you provide in the table schema, which is generally the original creation time of the data.
- Processing time: The local time at which the system processes an event.
The event time attribute of the Rowtime column is lost after a window operation. You
can use a helper function such as
SESSION_ROWTIME to obtain
max(rowtime) of the rowtime column in a window and use the obtained value as the rowtime of the
time window. The value is
window_end - 1 and is of the TIMESTAMP type with the rowtime attribute. For example, if the time
of a window is
00:14:59.999 is returned.
CREATE TABLE user_clicks( username varchar, click_url varchar, ts timeStamp, WATERMARK wk FOR ts as withOffset(ts, 2000) -- Define a watermark for rowtime. ) with ( type='datahub', ... ); CREATE TABLE tumble_output( window_start TIMESTAMP, window_end TIMESTAMP, username VARCHAR, clicks BIGINT ) with ( type='print' ); CREATE VIEW one_minute_window_output as SELECT // Use TUMBLE_ROWTIME as the aggregation time of the level-two window. TUMBLE_ROWTIME(ts, INTERVAL '1' MINUTE) as rowtime, username, COUNT(click_url) as cnt FROM user_clicks GROUP BY TUMBLE(ts, INTERVAL '1' MINUTE), username; INSERT INTO tumble_output SELECT TUMBLE_START(rowtime, INTERVAL '1' HOUR), TUMBLE_END(rowtime, INTERVAL '1' HOUR), username, SUM(cnt) FROM one_minute_window_output GROUP BY TUMBLE(rowtime, INTERVAL '1' HOUR), username