Datasets:
File size: 1,438 Bytes
e8c001c | 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | -- flinksql_010: 滑动窗口(HOP)店铺统计
-- datagen 生成订单流 @ 5000 rows/sec, HOP 1min slide 5min window
-- 1. 订单流 datagen 源表
CREATE TABLE order_source (
order_id BIGINT,
shop_id INT,
amount DOUBLE,
event_time AS LOCALTIMESTAMP,
WATERMARK FOR event_time AS event_time - INTERVAL '10' SECOND
) WITH (
'connector' = 'datagen',
'rows-per-second' = '5000',
'fields.order_id.kind' = 'random',
'fields.order_id.min' = '1',
'fields.order_id.max' = '1000000',
'fields.shop_id.kind' = 'random',
'fields.shop_id.min' = '1',
'fields.shop_id.max' = '100',
'fields.amount.kind' = 'random',
'fields.amount.min' = '0',
'fields.amount.max' = '1000'
);
-- 2. Console 输出表
CREATE TABLE console_output (
window_start TIMESTAMP,
window_end TIMESTAMP,
shop_id INT,
total_amount DOUBLE,
order_cnt BIGINT,
avg_amount DOUBLE
) WITH (
'connector' = 'print'
);
-- 3. HOP 滑动窗口聚合: 按店铺统计订单总额、数量和平均金额
INSERT INTO console_output
SELECT
HOP_START(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE) AS window_start,
HOP_END(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE) AS window_end,
shop_id,
SUM(amount) AS total_amount,
COUNT(*) AS order_cnt,
AVG(amount) AS avg_amount
FROM order_source
GROUP BY HOP(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE), shop_id;
|