Window TopN 定义(⽀持 Streaming): Window TopN 是特殊的 TopN,返回结果是每⼀个窗⼝内的 N 个最⼩值或者最⼤值。
应⽤场景: TopN 会出现中间结果,出现回撤数据,Window TopN 不会出现回撤数据,因为 Window TopN 是在窗⼝结束时输出最终结果,不会产⽣中间结果。
注意: 因为是窗⼝上⾯的操作, Window TopN 在窗⼝结束时,会⾃动把 State 清除。
SQL 语法标准:
SELECT [column_list]
FROM (
SELECT [column_list],
ROW_NUMBER() OVER (PARTITION BY window_start, window_end [, col_key1...]
ORDER BY col1 [asc|desc][, col2 [asc|desc]...]) AS rownum
FROM table_name) -- windowing TVF
WHERE rownum <= N [AND conditions]
实际案例: 取当前这⼀分钟的搜索关键词下的搜索热度前 10 名的词条数据。
输⼊表字段:
-- 字段名 备注
-- key 搜索关键词
-- name 搜索热度名称
-- search_cnt 热搜消费热度(⽐如 3000)
-- timestamp 消费词条时间戳
CREATE TABLE source_table (
name STRING NOT NULL,
search_cnt BIGINT NOT NULL,
key STRING NOT NULL,
row_time timestamp(3),
WATERMARK FOR row_time AS row_time
) WITH (
'connector' = 'filesystem',
'path' = 'file:///Users/hhx/Desktop/source_table.csv',
'format' = 'csv'
);
A,100,a,2021-11-01 00:01:00
B,200,b,2021-11-01 00:01:00
C,300,c,2021-11-01 00:01:00
D,400,d,2021-11-01 00:01:00
A,200,a,2021-11-01 00:01:05
B,300,b,2021-11-01 00:01:05
C,400,c,2021-11-01 00:01:05
D,500,d,2021-11-01 00:01:05
A,300,a,2021-11-01 00:02:00
B,400,b,2021-11-01 00:02:00
C,500,c,2021-11-01 00:02:00
D,600,d,2021-11-01 00:02:00
-- 输出表字段:
-- 字段名 备注
-- key 搜索关键词
-- name 搜索热度名称
-- search_cnt 热搜消费热度(⽐如 3000)
-- window_start 窗⼝开始时间戳
-- window_end 窗⼝结束时间戳
CREATE TABLE sink_table (
key BIGINT,
name BIGINT,
search_cnt BIGINT,
window_start TIMESTAMP(3),
window_end TIMESTAMP(3)
) WITH (
...
);
INSERT INTO sink_table
SELECT key, name, search_cnt, window_start, window_end
FROM (
SELECT key, name, search_cnt, window_start, window_end,
ROW_NUMBER() OVER (
PARTITION BY window_start, window_end, key
ORDER BY search_cnt desc) AS rownum
FROM (
SELECT window_start, window_end, key, name, max(search_cnt) as search_cnt
-- window tvf 写法
FROM TABLE(TUMBLE(TABLE source_table, DESCRIPTOR(row_time), INTERVAL '1' MINUTE))
GROUP BY window_start, window_end, key, name
)
)
WHERE rownum <= 2;
输出结果:
SQL 转换为算子: