| --- |
| id: online-compute_FlinkSQL_flinksql_002 |
| name: Order-Payment Interval Join + Windowed Aggregation |
| category: online-compute/FlinkSQL |
| timeout_seconds: 600 |
| modality: pure-text |
| engine: flink-sql |
| --- |
| ## Prompt |
| I need you to write a Flink SQL that uses two datagen built-in tables to simulate an order stream and a payment stream, performs an Interval Join on `order_id`, and then aggregates the order count and total payment amount over a 10-minute tumbling window, outputting the results to the console. |
|
|
| **Business Background and Objective**: Simulate an order stream and a payment stream using two datagen built-in tables, each generating 100 million records. The `order_id` and `user_id` in the order stream range from 1 to 1,000,000, and the `order_id` in the payment stream also ranges from 1 to 1,000,000. Both tables use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay. |
|
|
| Perform an Interval Join on the two tables using `order_id`, matching only records where the payment time falls within 10 minutes before or after the order time. Then, aggregate the successfully matched records over a 10-minute tumbling window to compute the order count and total payment amount per window, and finally output the results to the console table. |
|
|
| **Source Table Definitions**: |
| - `orders_source` (order stream, datagen connector): |
| - `order_id INT`: Order ID, randomly generated in the range 1–1,000,000 |
| - `user_id INT`: User ID, randomly generated in the range 1–1,000,000 |
| - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds |
| - Generation rate: `rows-per-second = 1000` |
|
|
| - `payments_source` (payment stream, datagen connector): |
| - `order_id INT`: Order ID, randomly generated in the range 1–1,000,000 |
| - `pay_amount DOUBLE`: Payment amount, randomly generated in the range 1–100,000 |
| - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds |
| - Generation rate: `rows-per-second = 1000` |
|
|
| **Output Table Definition**: |
| - `console_output` (print connector): |
| - `window_start VARCHAR`: Window start time |
| - `window_end VARCHAR`: Window end time |
| - `order_count BIGINT`: Order count within the window |
| - `total_pay_amount DOUBLE`: Total payment amount within the window |
|
|
| **Join + Aggregation Logic**: |
| - Interval Join condition: `orders_source.order_id = payments_source.order_id` |
| - Time window: `payments_source.event_time BETWEEN orders_source.event_time - INTERVAL '10' MINUTE AND orders_source.event_time + INTERVAL '10' MINUTE` |
| - Tumbling window: `TUMBLE` 10 minutes |
| - Aggregation metrics: `COUNT(*) AS order_count`, `SUM(pay_amount) AS total_pay_amount` |
| - Convert window times to `VARCHAR` for output |
|
|
| **Output Requirements**: |
| - Use `INSERT INTO console_output` to output the results. |
| - Output field order: `window_start`, `window_end`, `order_count`, `total_pay_amount` |
|
|