Datasets:
metadata
id: online-compute_FlinkSQL_flinksql_018
name: Dual Output (Detail + 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 the built-in datagen connector to simulate an order stream, simultaneously outputting detail records to console1 and windowed aggregation results over a 1-minute tumbling window to console2.
Business Background and Objective: Generate an order event stream using a datagen built-in table at a rate of 2000 rows per second. Fields include order_id (order ID), user_id (user ID, 1–10,000), and amount (order amount, 0–1000). Use LOCALTIMESTAMP as the event time and set a 5-second watermark delay.
The same source data must be simultaneously output to two sinks:
console1: Detail passthrough, directly outputting the original fieldsconsole2: Aggregation over a 1-minuteTUMBLEtumbling window, grouped byuser_idto compute the order count and total amount
Source Table Definition:
order_source(datagen connector):order_id BIGINT: Order ID, randomly generateduser_id INT: User ID, randomly generated in the range 1–10,000amount DOUBLE: Order amount, randomly generated in the range 0–1000event_time: UsesLOCALTIMESTAMPto generate event time, with a WATERMARK delay of 5 seconds- Generation rate:
rows-per-second = 2000
Output Table 1 Definition:
console1(print connector, detail output):order_id BIGINTuser_id INTamount DOUBLE
Output Table 2 Definition:
console2(print connector, aggregation output):window_start TIMESTAMP(3): Window start timeuser_id INT: User IDorder_cnt BIGINT: Order count within the windowtotal_amount DOUBLE: Total order amount within the window
Aggregation Logic:
- Detail output: Direct passthrough from
order_source - Aggregation output:
TUMBLE1-minute tumbling window,GROUP BY user_id - Aggregation metrics:
COUNT(*) AS order_cnt,SUM(amount) AS total_amount
Output Requirements:
- Use two
INSERT INTOstatements to write toconsole1andconsole2respectively. console1output field order:order_id,user_id,amountconsole2output field order:window_start,user_id,order_cnt,total_amount