75. Click Counts in 5-Minute Windows with Late Data
Difficulty: Medium · Topics: Streaming, Aggregations, Filtering & Selection
Clicks arrive as a stream clicks (page, ts), where ts is when the click happened. Phones send clicks late and out of order.
Count clicks per page in 5-minute tumbling windows of event time. Accept clicks that are up to 10 minutes late: use a 10-minute watermark on ts, so a window is final once the watermark passes its end, and clicks for a window that is already final are dropped.
Each window must be written exactly once, when it is final. Return window_start, window_end, page, clicks.
The stream arrives in micro-batches: batch in the preview is the batch an event arrives in, not a column of the stream. The expected output lists every row your query writes, with batch set to the micro-batch after which it is written.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
clicks
| batch | page | ts |
|---|---|---|
| 1 | home | 2024-05-01 10:01:00 |
| 1 | home | 2024-05-01 10:03:00 |
| 1 | docs | 2024-05-01 10:04:00 |
| 1 | home | 2024-05-01 10:07:00 |
| 2 | home | 2024-05-01 10:12:00 |
| 2 | docs | 2024-05-01 10:16:00 |
| 3 | home | 2024-05-01 10:04:00 |
| 3 | docs | 2024-05-01 10:09:00 |
| 3 | home | 2024-05-01 10:27:00 |
| 4 | docs | 2024-05-01 10:41:00 |
Expected output
| batch | window_start | window_end | page | clicks |
|---|---|---|---|---|
| 2 | 2024-05-01 10:00:00 | 2024-05-01 10:05:00 | home | 2 |
| 2 | 2024-05-01 10:00:00 | 2024-05-01 10:05:00 | docs | 1 |
| 3 | 2024-05-01 10:05:00 | 2024-05-01 10:10:00 | home | 1 |
| 3 | 2024-05-01 10:10:00 | 2024-05-01 10:15:00 | home | 1 |
| 3 | 2024-05-01 10:05:00 | 2024-05-01 10:10:00 | docs | 1 |
| 4 | 2024-05-01 10:15:00 | 2024-05-01 10:20:00 | docs | 1 |
| 4 | 2024-05-01 10:25:00 | 2024-05-01 10:30:00 | home | 1 |
Hints
Hint 1
Group by an event-time window:groupBy(F.window("ts", "5 minutes"), "page"). The window column is a struct with start and end fields, read with F.col("window.start").Hint 2
CallwithWatermark("ts", "10 minutes") before the groupBy. Spark sets the watermark to the latest event time seen minus 10 minutes, at the end of each batch.Hint 3
Written once, when final, isappend mode. With a watermark, append mode writes a window when the watermark passes its end, in the extra batch Spark runs right after the watermark moves.Learn the concepts
- Event-time windows · 7 min read. Tumbling, sliding and session windows, what they cost in state, and when a window is final.
- Watermarks and late data · 8 min read. Event time vs processing time, how watermarks bound state, and what happens to late rows.
- Structured Streaming basics · 8 min read. Streams as an unbounded table: sources, sinks, triggers, output modes and checkpoints.
PySpark functions you'll practise
- groupBy
- agg
- count
- select
- withWatermark
- writeStream
Related problems
- Hourly Revenue per Store, Updated Live · Medium · Streaming, Aggregations, Filtering & Selection
- Drop Duplicate Events from a Retrying Producer · Medium · Streaming, Filtering & Selection
- Trending Searches in Sliding Windows · Hard · Streaming, Aggregations, Filtering & Selection
- Live Page View Counts · Easy · Streaming, Aggregations
- Enrich a Stream of Orders with Customer Data · Easy · Streaming, Joins, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice