| --- |
| id: online-compute_FlinkSQL_flinksql_001 |
| name: Order Stream and Payment Stream Interval Join |
| 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 stream and a payment stream, performing an Interval Join on `order_id`, and writing matched records directly 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 5 minutes after the order time. Upon a successful match, output the original fields directly to the console table, including `order_id`, `user_id`, `pay_amount`, the order time, and the payment time. |
|
|
| **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 |
| - `order_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 |
| - `pay_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): |
| - `order_id INT` |
| - `user_id INT` |
| - `pay_amount DOUBLE` |
| - `order_time TIMESTAMP(3)` |
| - `pay_time TIMESTAMP(3)` |
|
|
| **Join Logic**: |
| - Join condition: `orders_source.order_id = payments_source.order_id` |
| - Time window: `payments_source.pay_time BETWEEN orders_source.order_time AND orders_source.order_time + INTERVAL '5' MINUTE` |
| - Note: When writing to the sink, you must handle the dual rowtime attribute conflict by applying `CAST(pay_time AS TIMESTAMP(3))` to downgrade `pay_time` to a plain timestamp. |
|
|
| **Output Requirements**: |
| - Use `INSERT INTO console_output` to output the matched results. |
| - Output field order: `order_id`, `user_id`, `pay_amount`, `order_time`, `pay_time` |
|
|