File size: 1,867 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
---
id: online-compute_FlinkSQL_flinksql_009
name: Tumbling Window PV/UV Statistics
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 simulate a user click stream, computes PV and UV per page over a window, and writes the results to the console.

**Business Background and Objective**: Simulate a user click stream using a datagen built-in table (`user_id` ranging from 1–1000, `page_id` ranging from 1–50, 10,000 records per second). Compute the PV (`COUNT(*)`) and UV (`COUNT(DISTINCT user_id)`) per page over a 1-minute tumbling window. Output the results (window start, window end, `page_id`, `pv`, `uv`) to the console.

**Source Table Definition**:
- `click_source` (user click stream, datagen connector):
  - `user_id INT`: User ID, randomly generated in the range 1–1000
  - `page_id INT`: Page ID, randomly generated in the range 1–50
  - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds
  - Generation rate: `rows-per-second = 10000`

**Output Table Definition**:
- `console_output` (print connector):
  - `window_start TIMESTAMP`: Window start time
  - `window_end TIMESTAMP`: Window end time
  - `page_id INT`: Page ID
  - `pv BIGINT`: Page views (PV)
  - `uv BIGINT`: Unique visitors (UV)

**Query Logic**:
- Window: `TUMBLE(event_time, INTERVAL '1' MINUTE)` tumbling window
- Grouping: Group by `page_id` and the window
- PV: `COUNT(*)` to compute page views
- UV: `COUNT(DISTINCT user_id)` to compute unique visitors
- Window functions: Use `TUMBLE_START` / `TUMBLE_END` to obtain the window start and end times

**Output Requirements**:
- Use `INSERT INTO console_output` to output the results.
- Output field order: `window_start`, `window_end`, `page_id`, `pv`, `uv`