Datasets:
metadata
id: online-compute_FlinkSQL_flinksql_012
name: Session Window User Behavior 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 the built-in datagen connector to simulate a user behavior data source, computes per-user behavioral statistics over a 30-minute session window (SESSION), and outputs the results to the console.
Business Background and Objective: Simulate a user behavior stream using a datagen built-in table. Group by a 30-minute session window to compute the total action count (COUNT) and the number of distinct action types (COUNT DISTINCT) per user within each session window. Output the results to the console.
Source Table Definition:
user_actions(user behavior stream, datagen connector):user_id INT: User ID, randomly generated in the range 1–5000action_id INT: Action type ID, randomly generated in the range 1–10event_time: UsesLOCALTIMESTAMPto generate event time, with a WATERMARK delay of 5 seconds- Generation rate:
rows-per-second = 2000
Output Table Definition:
console_output(print connector):user_id INT: User IDsession_start TIMESTAMP(3): Session window start timesession_end TIMESTAMP(3): Session window end timeaction_count BIGINT: Total action countunique_actions BIGINT: Number of distinct action types
SQL Logic:
- Apply
SESSION(event_time, INTERVAL '30' MINUTE)session window onuser_actions - Group by
SESSION(event_time, INTERVAL '30' MINUTE)anduser_id - Aggregate
COUNT(*) AS action_countandCOUNT(DISTINCT action_id) AS unique_actions - Use
SESSION_STARTandSESSION_ENDfunctions to obtain the window start and end times
Output Requirements:
- Use
INSERT INTO console_outputto output the results. - Output field order:
user_id,session_start,session_end,action_count,unique_actions