头图

一、参考

flink 学习系列目录 ——更新ing

Flink 中极其重要的 Time 与 Window 详细解析

Window Aggregation

阿里云: 窗口函数

二、window

三、示例

3.1 准备csv文件

bid.csv 如下

supplier1,C,40,2020-04-15 08:05:00.0
supplier1,A,20,2020-04-15 08:07:00.0
supplier2,D,50,2020-04-15 08:09:00.0
supplier2,B,30,2020-04-15 08:11:00.0
supplier1,E,10,2020-04-15 08:13:00.0
supplier2,F,20,2020-04-15 08:17:00.0

3.2 创建table

/*
    创建bid表
     注意:
     (1) csv 文件中不需要添加header;
    (2) timestamp(3) 的格式为 2021-09-02 01:00:00.000, 即表示可以精确到秒后小数点3位
*/
create table bid (
       supplier_id STRING,
       item STRING,
       price INT,
       bidtime TIMESTAMP(3),
       WATERMARK FOR bidtime AS bidtime - INTERVAL '1' SECOND
) with (
  'connector' = 'filesystem',
  'path' = '/Users/yz/work/github/flinkLearn/csvs/bid.csv',
  'format' = 'csv'
);

3.3 hop滑动窗口

/*
    bid 查询语句, 滑动窗口
*/
select window_start, window_end,sum(price) from table(
       hop(table bid, descriptor(bidtime), interval '5' minutes, interval '10' minutes)
)
group by window_start, window_end;
select hop_start(bidtime, interval '2' minute, interval '4' minute) as hop_start,
       hop_end(bidtime, interval '2' minute, interval '4' minute) as hop_end,
       sum(price) as sum_price from bid group by hop(bidtime, interval '2' minute, interval '4' minute);

image.png


一曲广陵散
76 声望21 粉丝

柴米油盐酱醋茶