78. Trending Searches in Sliding Windows
Difficulty: Hard · Topics: Streaming, Aggregations, Filtering & Selection
Searches arrive as a stream searches (term, ts). A term is trending in a window when it was searched at least 2 times in it.
Use 10-minute windows that start every 5 minutes (so every search counts in two windows), with a 5-minute watermark on ts. Write each trending (window, term) exactly once, when its window is final. Return window_start, window_end, term, searches.
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
searches
| batch | term | ts |
|---|---|---|
| 1 | spark | 2024-05-01 10:01:00 |
| 1 | spark | 2024-05-01 10:04:00 |
| 1 | dbt | 2024-05-01 10:06:00 |
| 1 | spark | 2024-05-01 10:07:00 |
| 2 | dbt | 2024-05-01 10:08:00 |
| 2 | spark | 2024-05-01 10:12:00 |
| 3 | dbt | 2024-05-01 10:30:00 |
Expected output
| batch | window_start | window_end | term | searches |
|---|---|---|---|---|
| 2 | 2024-05-01 09:55:00 | 2024-05-01 10:05:00 | spark | 2 |
| 3 | 2024-05-01 10:00:00 | 2024-05-01 10:10:00 | spark | 3 |
| 3 | 2024-05-01 10:00:00 | 2024-05-01 10:10:00 | dbt | 2 |
| 3 | 2024-05-01 10:05:00 | 2024-05-01 10:15:00 | dbt | 2 |
| 3 | 2024-05-01 10:05:00 | 2024-05-01 10:15:00 | spark | 2 |
Hints
Hint 1
A sliding window takes a third argument, the slide:F.window("ts", "10 minutes", "5 minutes"). A search at 10:07 falls in [10:00, 10:10) and [10:05, 10:15).Hint 2
Filter after the aggregation:.filter(F.col("searches") >= 2). A filter keeps no state, so it is fine in append mode.Hint 3
Written once when final isappend mode, which needs the watermark before the groupBy.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
- filter
- select
- withWatermark
- writeStream
Related problems
- 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
- 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