File size: 3,020 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
49
50
51
52
53
54
---
id: online-compute_FlinkSQL_flinksql_004
name: Dual Window Aggregation (Tumble + Hop)
category: online-compute/FlinkSQL
timeout_seconds: 600
modality: pure-text
engine: flink-sql
---
## Prompt
I need you to write a Flink SQL that uses a datagen built-in table to generate order events, and computes the sales volume and order count per product using both a tumbling window and a sliding window, outputting the results to two separate console tables.

**Business Background and Objective**: Generate order events (`order_id BIGINT`, `product_id INT`, `quantity INT`) using a datagen built-in table. Use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay.

Compute statistics using two window types:
1. **1-minute tumbling window**: Compute the sales volume `SUM(quantity) AS sales` and order count `COUNT(*) AS order_cnt` per product.
2. **Sliding window** (slides every 30 seconds, covering the past 1 minute): Compute the sales volume `SUM(quantity) AS sales` and order count `COUNT(*) AS order_cnt` per product.

Print the results of both window types to two separate console tables, with each record carrying a window type identifier `window_type`.

**Source Table Definition**:
- `orders_source` (order event stream, datagen connector):
  - `order_id BIGINT`: Order ID, randomly generated in the range 1–1,000,000
  - `product_id INT`: Product ID, randomly generated in the range 1–10,000
  - `quantity INT`: Quantity, randomly generated in the range 1–10
  - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds
  - Generation rate: `rows-per-second = 50`

**Output Table Definitions**:
- `console_tumble` (print connector, tumbling window results):
  - `window_type STRING`: Window type identifier, fixed as `'TUMBLE'`
  - `window_start TIMESTAMP(3)`: Window start time
  - `window_end TIMESTAMP(3)`: Window end time
  - `product_id INT`: Product ID
  - `sales INT`: Sales volume of the product within the window
  - `order_cnt BIGINT`: Order count of the product within the window

- `console_hop` (print connector, sliding window results):
  - `window_type STRING`: Window type identifier, fixed as `'HOP'`
  - `window_start TIMESTAMP(3)`: Window start time
  - `window_end TIMESTAMP(3)`: Window end time
  - `product_id INT`: Product ID
  - `sales INT`: Sales volume of the product within the window
  - `order_cnt BIGINT`: Order count of the product within the window

**Aggregation Logic**:
- Tumbling window: `GROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), product_id`
- Sliding window: `GROUP BY HOP(event_time, INTERVAL '30' SECOND, INTERVAL '1' MINUTE), product_id`
- Aggregation metrics: `SUM(quantity) AS sales`, `COUNT(*) AS order_cnt`
- Both window times and the window type must be explicitly output

**Output Requirements**:
- Use two `INSERT INTO` statements to output to `console_tumble` and `console_hop` respectively.
- Output field order: `window_type`, `window_start`, `window_end`, `product_id`, `sales`, `order_cnt`