File size: 2,366 Bytes
e8c001c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
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 fields
- `console2`: Aggregation over a 1-minute `TUMBLE` tumbling window, grouped by `user_id` to compute the order count and total amount

**Source Table Definition**:
- `order_source` (datagen connector):
  - `order_id BIGINT`: Order ID, randomly generated
  - `user_id INT`: User ID, randomly generated in the range 1–10,000
  - `amount DOUBLE`: Order amount, randomly generated in the range 0–1000
  - `event_time`: Uses `LOCALTIMESTAMP` to 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 BIGINT`
  - `user_id INT`
  - `amount DOUBLE`

**Output Table 2 Definition**:
- `console2` (print connector, aggregation output):
  - `window_start TIMESTAMP(3)`: Window start time
  - `user_id INT`: User ID
  - `order_cnt BIGINT`: Order count within the window
  - `total_amount DOUBLE`: Total order amount within the window

**Aggregation Logic**:
- Detail output: Direct passthrough from `order_source`
- Aggregation output: `TUMBLE` 1-minute tumbling window, `GROUP BY user_id`
- Aggregation metrics: `COUNT(*) AS order_cnt`, `SUM(amount) AS total_amount`

**Output Requirements**:
- Use two `INSERT INTO` statements to write to `console1` and `console2` respectively.
- `console1` output field order: `order_id`, `user_id`, `amount`
- `console2` output field order: `window_start`, `user_id`, `order_cnt`, `total_amount`