Window Aggregation
Window TVF Aggregation
Batch Streaming
Window aggregations are defined in the GROUP BY clause contains “window_start” and “window_end” columns of the relation applied Windowing TVF. Just like queries with regular GROUP BY clauses, queries with a group by window aggregation will compute a single result row per group.
窗口聚合在GROUP BY子句中定义,该子句包含应用窗口化TVF的关系的“window_start”和“window_end”列。就像使用常规GROUP BY子句的查询一样,使用窗口聚合的查询将计算每个组的单个结果行。
SELECT ...
FROM <windowed_table> -- relation applied windowing TVF
GROUP BY window_start, window_end, ...
Unlike other aggregations on continuous tables, window aggregation do not emit intermediate results but only a final result, the total aggregation at the end of the window. Moreover, window aggregations purge all intermediate state when no longer needed.
与持续表上的其他聚合不同,窗口聚合不发出中间结果,而只发出最终结果,即窗口末尾的总聚合。此外,窗口聚合在不再需要时清除所有中间状态。
Windowing TVFs
Flink supports TUMBLE, HOP and CUMULATE types of window aggregations. In streaming mode, the time attribute field of a window table-valued function must be on either event or processing time attributes. See Windowing TVF for more windowing functions information. In batch mode, the time attribute field of a window table-valued function must be an attribute of type TIMESTAMP or TIMESTAMP_LTZ.
Flink支持TUMBLE、HOP和CUMULATE类型的窗口聚合。在流模式下,窗口化表值函数的时间属性字段必须位于事件或处理时间属性上。有关窗口功能的更多信息,请参见窗口化TVF。在批处理模式下,窗口化表值函数的时间属性字段必须是TIMESTAMP或TIMESTAMP_LTZ类型的属性。
Here are some examples for TUMBLE, HOP and CUMULATE window aggregations.
下面是一些TUMBLE、HOP和CUMULATE窗口聚合的示例。
-- tables must have time attribute, e.g. `bidtime` in this table
Flink SQL> desc Bid;
+-------------+------------------------+------+-----+--------+---------------------------------+
| name | type | null | key | extras | watermark |
+-------------+------------------------+------+-----+--------+---------------------------------+
| bidtime | TIMESTAMP(3) *ROWTIME* | true | | | `bidtime` - INTERVAL '1' SECOND |
| price | DECIMAL(10, 2) | true | | | |
| item | STRING | true | | | |
| supplier_id | STRING | true | | | |
+-------------+------------------------+------+-----+--------+---------------------------------+
Flink SQL> SELECT * FROM Bid;
+------------------+-------+------+-------------+
| bidtime | price | item | supplier_id |
+------------------+-------+------+-------------+
| 2020-04-15 08:05 | 4.00 | C | supplier1 |
| 2020-04-15 08:07 | 2.00 | A | supplier1 |
| 2020-04-15 08:09 | 5.00 | D | supplier2 |
| 2020-04-15 08:11 | 3.00 | B | supplier2 |
| 2020-04-15 08:13 | 1.00 | E | supplier1 |
| 2020-04-15 08:17 | 6.00 | F | supplier2 |
+------------------+-------+------+-------------+
-- tumbling window aggregation
Flink SQL> SELECT window_start, window_end, SUM(price)
FROM TABLE(
TUMBLE(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '10' MINUTES)