Datasets:
metadata
id: online-compute_FlinkSQL_flinksql_011
name: Tumbling Window Product TopN Ranking
category: online-compute/FlinkSQL
timeout_seconds: 600
modality: pure-text
engine: flink-sql
Prompt
I need you to write a Flink SQL that uses the built-in datagen connector to simulate an order data source, aggregates product sales over a 10-minute tumbling window, and then uses the ROW_NUMBER window function to retrieve the top 5 products by sales within each window, outputting the results to the console.
Business Background and Objective: Simulate an order stream using a datagen built-in table, generating massive order data. Group by a 10-minute tumbling window to compute the total sales amount and order count per product within each window, and finally use ROW_NUMBER ranking to retrieve the top 5 products by sales amount within each window.
Source Table Definition:
order_source(order stream, datagen connector):order_id BIGINT: Order ID, randomly generated in the range 1–10,000,000product_id INT: Product ID, randomly generated in the range 1–200amount DOUBLE: Order amount, randomly generated in the range 0–500event_time: UsesLOCALTIMESTAMPto generate event time, with a WATERMARK delay of 5 seconds- Generation rate:
rows-per-second = 8000
Output Table Definition:
console_output(print connector):window_start TIMESTAMP(3): Tumbling window start timeproduct_id INT: Product IDsales DOUBLE: Total sales amount of the product within the windoworder_cnt BIGINT: Number of orders for the product within the windowrn BIGINT: Sales ranking within the window
SQL Logic:
- Apply
TUMBLE(event_time, INTERVAL '10' MINUTE)tumbling window onorder_source - Group by
window_start,window_end,product_id - Aggregate
SUM(amount) AS salesandCOUNT(*) AS order_cnt - Use
ROW_NUMBER() OVER (PARTITION BY window_start, window_end ORDER BY sales DESC) AS rn - Outer query filter:
WHERE rn <= 5 - Final SELECT:
window_start,product_id,sales,order_cnt,rn
Output Requirements:
- Use
INSERT INTO console_outputto output the results. - Output field order:
window_start,product_id,sales,order_cnt,rn