| --- |
| id: online-compute_FlinkSQL_flinksql_003 |
| name: Word Frequency Tumbling Window 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 word frequency data stream, groups by `word` and opens a 1-minute tumbling window to count the occurrences of each word, and outputs the results to the console. |
|
|
| **Business Background and Objective**: Simulate a word frequency data stream using a datagen built-in table, generating 1 billion records in the `word` field. Use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay. Group by `word` and count the occurrences of each word over a 1-minute tumbling window, then output `word` and `cnt` to the console table. |
|
|
| **Source Table Definition**: |
| - `word_source` (word frequency stream, datagen connector): |
| - `word VARCHAR`: Word, randomly generated (value range 1–1000 to simulate distinct words) |
| - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds |
| - Generation rate: `rows-per-second = 1000` |
|
|
| **Output Table Definition**: |
| - `console_output` (print connector): |
| - `word VARCHAR`: Word |
| - `cnt BIGINT`: Number of occurrences of the word within the current window |
|
|
| **Aggregation Logic**: |
| - Group by `word`, using a 1-minute tumbling window |
| - Aggregation metric: `COUNT(*) AS cnt` |
| - The window is defined by the `TUMBLE` function |
|
|
| **Output Requirements**: |
| - Use `INSERT INTO console_output` to output the results. |
| - Output field order: `word`, `cnt` |
|
|