Datasets:
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,000user_id INT: User ID, randomly generated in the range 1–1,000,000event_time: UsesLOCALTIMESTAMPto 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,000pay_amount DOUBLE: Payment amount, randomly generated in the range 1–100,000event_time: UsesLOCALTIMESTAMPto 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 timewindow_end VARCHAR: Window end timeorder_count BIGINT: Order count within the windowtotal_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:
TUMBLE10 minutes - Aggregation metrics:
COUNT(*) AS order_count,SUM(pay_amount) AS total_pay_amount - Convert window times to
VARCHARfor output
Output Requirements:
- Use
INSERT INTO console_outputto output the results. - Output field order:
window_start,window_end,order_count,total_pay_amount