74. Enrich a Stream of Orders with Customer Data
Difficulty: Easy · Topics: Streaming, Joins, Filtering & Selection
Orders arrive as a stream orders (order_id, customer_id, amount, ts). customers (customer_id, country, tier) is an ordinary, static DataFrame.
For every order with a positive amount (the shop sends test orders with an amount of 0 or less), write order_id, customer_id, amount, country, tier as soon as it arrives. A new customer may not be in customers yet: keep their orders, with country and tier null.
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
orders
| batch | order_id | customer_id | amount | ts |
|---|---|---|---|---|
| 1 | 101 | 1 | 499 | 2024-05-01 10:00:00 |
| 1 | 102 | 3 | 120.5 | 2024-05-01 10:01:00 |
| 2 | 103 | 2 | -1 | 2024-05-01 10:02:00 |
| 2 | 104 | 4 | 75 | 2024-05-01 10:03:00 |
| 3 | 105 | 1 | 20 | 2024-05-01 10:05:00 |
customers
| customer_id | country | tier |
|---|---|---|
| 1 | IN | gold |
| 2 | US | silver |
| 3 | DE | silver |
Expected output
| batch | order_id | customer_id | amount | country | tier |
|---|---|---|---|---|---|
| 1 | 101 | 1 | 499 | IN | gold |
| 1 | 102 | 3 | 120.5 | DE | silver |
| 2 | 104 | 4 | 75 | null | null |
| 3 | 105 | 1 | 20 | IN | gold |
Hints
Hint 1
A stream can be joined to a static DataFrame like any other join: each micro-batch is joined to the whole static table.Hint 2
An inner join drops the orders of customers not incustomers; keep them with a "left" join.Hint 3
A filter and a join keep no state, soappend mode writes each order once, in the batch it arrives in.Learn the concepts
- Stream joins · 7 min read. Stream-static enrichment, stream-stream joins bounded by watermarks, and why outer joins emit late.
- join · 5 min read. Combine DataFrames on a key: inner, left, right and full joins, and the duplicate-key fan-out trap.
- Structured Streaming basics · 8 min read. Streams as an unbounded table: sources, sinks, triggers, output modes and checkpoints.
PySpark functions you'll practise
- join
- filter
- select
- writeStream
Related problems
- Live Page View Counts · Easy · Streaming, Aggregations
- 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