窗口
GROUP WINDOW
语法说明
Group Window定义在GROUP BY里,每个分组只输出一条记录,包括以下几种:
- time_attr可以设置processing-time或者event-time。
- time_attr设置为event-time时参数类型为bigint或者timestamp类型。
- time_attr设置为processing-time时无需指定类型。
- interval设置窗口周期。
- 分组函数
表1 分组函数表 函数名
说明
TUMBLE(time_attr, interval)
跳跃窗口。
HOP(time_attr, interval, interval)
拓展的跳跃窗口(等价于datastream的滑动窗口),可以分别设置输出触发周期和窗口周期。
SESSION(time_attr, interval)
会话窗口,interval表示多长时间没有记录则关闭窗口。
- 窗口函数
表2 窗口函数表 函数名
说明
TUMBLE_START(time_attr, interval)
返回跳跃窗口开始时间。为UTC时区。
TUMBLE_END(time_attr, interval)
返回跳跃窗口结束时间。为UTC时区。
HOP_START(time_attr, interval, interval)
返回拓展的跳跃窗口开始时间。为UTC时区。
HOP_END(time_attr, interval, interval)
返回拓展的跳跃窗口结束时间。为UTC时区。
SESSION_START(time_attr, interval)
返回会话窗口开始时间。为UTC时区。
SESSION_END(time_attr, interval)
返回会话窗口结束时间。为UTC时区。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// 每天计算SUM(金额)(事件时间)。 insert into temp SELECT name, TUMBLE_START(ts, INTERVAL '1' DAY) as wStart, SUM(amount) FROM Orders GROUP BY TUMBLE(ts, INTERVAL '1' DAY), name; // 每天计算SUM(金额)(处理时间)。 insert into temp SELECT name, SUM(amount) FROM Orders GROUP BY TUMBLE(proctime, INTERVAL '1' DAY), name; // 每个小时计算事件时间中最近24小时的SUM(数量)。 insert into temp SELECT product, SUM(amount) FROM Orders GROUP BY HOP(ts, INTERVAL '1' HOUR, INTERVAL '1' DAY), product; // 计算每个会话的SUM(数量),间隔12小时的不活动间隙(事件时间)。 insert into temp SELECT name, SESSION_START(ts, INTERVAL '12' HOUR) AS sStart, SESSION_END(ts, INTERVAL '12' HOUR) AS sEnd, SUM(amount) FROM Orders GROUP BY SESSION(ts, INTERVAL '12' HOUR), name; |
OVER WINDOW
Over Window与Group Window区别在于Over window每一行都会输出一条记录。
语法格式
1 2 3 4 |
OVER ( [PARTITION BY partition_name] ORDER BY proctime|rowtime(ROWS number PRECEDING) |(RANGE (BETWEEN INTERVAL '1' SECOND PRECEDING AND CURRENT ROW | UNBOUNDED preceding)) ) |
语法说明
参数 |
参数说明 |
---|---|
PARTITION BY |
指定分组的主键,每个分组各自进行计算。 |
ORDER BY |
指定数据按processing time或event time作为时间戳。 |
ROWS |
个数窗口。 |
RANGE |
时间窗口。 |
注意事项
- 同一select里所有聚合函数定义的窗口都必须保持一致。
- 当前Over窗口只支持前向计算(preceding),不支持following计算。
- 必须指定ORDER BY 按processing time或event time。
- 不支持对常量做聚合操作,如sum(2)。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// 计算从规则启动到目前为止的计数及总和(in proctime) insert into temp SELECT name, count(amount) OVER (PARTITION BY name ORDER BY proctime RANGE UNBOUNDED preceding) as cnt1, sum(amount) OVER (PARTITION BY name ORDER BY proctime RANGE UNBOUNDED preceding) as cnt2 FROM Orders; // 计算最近四条记录的计数及总和(in proctime) insert into temp SELECT name, count(amount) OVER (PARTITION BY name ORDER BY proctime ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) as cnt1, sum(amount) OVER (PARTITION BY name ORDER BY proctime ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) as cnt2 FROM Orders; // 计算最近60s的计数及总和(in eventtime),基于事件时间处理,事件时间为Orders中的timeattr字段。 insert into temp SELECT name, count(amount) OVER (PARTITION BY name ORDER BY timeattr RANGE BETWEEN INTERVAL '60' SECOND PRECEDING AND CURRENT ROW) as cnt1, sum(amount) OVER (PARTITION BY name ORDER BY timeattr RANGE BETWEEN INTERVAL '60' SECOND PRECEDING AND CURRENT ROW) as cnt2 FROM Orders; |