73. Live Page View Counts
Difficulty: Easy · Topics: Streaming, Aggregations
Page views arrive as a stream views (page, ts). A live dashboard keeps a running total of views per page since the stream started.
The dashboard only wants what changed: after each micro-batch, write the current total of every page that got new views in that batch, and nothing for pages that did not. Return page, views, and start the query with the output mode that does this.
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
views
| batch | page | ts |
|---|---|---|
| 1 | home | 2024-05-01 10:00:00 |
| 1 | home | 2024-05-01 10:01:00 |
| 1 | pricing | 2024-05-01 10:02:00 |
| 2 | home | 2024-05-01 10:05:00 |
| 2 | docs | 2024-05-01 10:06:00 |
| 3 | pricing | 2024-05-01 10:09:00 |
| 3 | pricing | 2024-05-01 10:10:00 |
Expected output
| batch | page | views |
|---|---|---|
| 1 | home | 2 |
| 1 | pricing | 1 |
| 2 | home | 3 |
| 2 | docs | 1 |
| 3 | pricing | 3 |
Hints
Hint 1
Count per page withgroupBy("page").count(), then rename the count to views.Hint 2
There are three output modes:complete writes the whole result table after every batch, update writes only the rows that changed, and append writes rows that will never change again.Hint 3
Start the query:result.writeStream.outputMode("…").start(). Append mode is refused for an aggregation without a watermark, because a running total can always change.Learn the concepts
- Output modes: append, update, complete · 6 min read. What each mode writes after a batch, which queries allow which mode, and what the sink must support.
- Structured Streaming basics · 8 min read. Streams as an unbounded table: sources, sinks, triggers, output modes and checkpoints.
- groupBy + agg · 4 min read. Aggregate rows per group: counts, sums, averages, and how count(*) differs from count(col).
PySpark functions you'll practise
- groupBy
- agg
- count
- writeStream
Related problems
- Enrich a Stream of Orders with Customer Data · Easy · Streaming, Joins, Filtering & Selection
- Click Counts in 5-Minute Windows with Late Data · Medium · Streaming, Aggregations, Filtering & Selection
- 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
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