Datasets:
id: online-compute_FlinkSQL_flinksql_004
name: Dual Window Aggregation (Tumble + Hop)
category: online-compute/FlinkSQL
timeout_seconds: 600
modality: pure-text
engine: flink-sql
Prompt
I need you to write a Flink SQL that uses a datagen built-in table to generate order events, and computes the sales volume and order count per product using both a tumbling window and a sliding window, outputting the results to two separate console tables.
Business Background and Objective: Generate order events (order_id BIGINT, product_id INT, quantity INT) using a datagen built-in table. Use LOCALTIMESTAMP as the event time and set a 5-second watermark delay.
Compute statistics using two window types:
- 1-minute tumbling window: Compute the sales volume
SUM(quantity) AS salesand order countCOUNT(*) AS order_cntper product. - Sliding window (slides every 30 seconds, covering the past 1 minute): Compute the sales volume
SUM(quantity) AS salesand order countCOUNT(*) AS order_cntper product.
Print the results of both window types to two separate console tables, with each record carrying a window type identifier window_type.
Source Table Definition:
orders_source(order event stream, datagen connector):order_id BIGINT: Order ID, randomly generated in the range 1–1,000,000product_id INT: Product ID, randomly generated in the range 1–10,000quantity INT: Quantity, randomly generated in the range 1–10event_time: UsesLOCALTIMESTAMPto generate event time, with a WATERMARK delay of 5 seconds- Generation rate:
rows-per-second = 50
Output Table Definitions:
console_tumble(print connector, tumbling window results):window_type STRING: Window type identifier, fixed as'TUMBLE'window_start TIMESTAMP(3): Window start timewindow_end TIMESTAMP(3): Window end timeproduct_id INT: Product IDsales INT: Sales volume of the product within the windoworder_cnt BIGINT: Order count of the product within the window
console_hop(print connector, sliding window results):window_type STRING: Window type identifier, fixed as'HOP'window_start TIMESTAMP(3): Window start timewindow_end TIMESTAMP(3): Window end timeproduct_id INT: Product IDsales INT: Sales volume of the product within the windoworder_cnt BIGINT: Order count of the product within the window
Aggregation Logic:
- Tumbling window:
GROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), product_id - Sliding window:
GROUP BY HOP(event_time, INTERVAL '30' SECOND, INTERVAL '1' MINUTE), product_id - Aggregation metrics:
SUM(quantity) AS sales,COUNT(*) AS order_cnt - Both window times and the window type must be explicitly output
Output Requirements:
- Use two
INSERT INTOstatements to output toconsole_tumbleandconsole_hoprespectively. - Output field order:
window_type,window_start,window_end,product_id,sales,order_cnt