An event is a small record of one thing that happened, such as an order being placed: {"event_id": "e1", "order_id": 5001, "amount": 50, "event_time": "2026-05-01 10:00:05"}. A batch job waits until the day is over, then reads all of the day's events at once. A stream hands you one event, then the next, as they happen, and never tells you which one is the last.
To answer questions about a stream, you keep a small state: the few values you need, updated as each event arrives. A running total is the simplest kind. Here a stream keeps the biggest order seen so far, and prints it after every event:
biggest = 0
for event in events:
if event["amount"] > biggest:
biggest = event["amount"]
print(event["event_id"], "| biggest so far:", biggest)
Why a data engineer needs it: a sales dashboard fed by a nightly batch job is up to a day behind. A stream's answer is up to date after every event, but never final, since another event can always come. Your program must print the stream's state after each event, then the batch job's answer:
Stream, after each event:
2026-05-01 10:00:05 e1 | orders: 1 | revenue: 50
2026-05-01 10:00:40 e2 | orders: 2 | revenue: 70
2026-05-01 10:01:10 e3 | orders: 3 | revenue: 105
2026-05-01 10:01:45 e4 | orders: 4 | revenue: 117
2026-05-01 10:02:20 e5 | orders: 5 | revenue: 177
2026-05-01 10:02:50 e6 | orders: 6 | revenue: 202
2026-05-01 10:03:30 e7 | orders: 7 | revenue: 242
2026-05-01 10:04:10 e8 | orders: 8 | revenue: 257
Batch job, once the day is over:
orders: 8 | revenue: 257
In this chapter, a for event in events: loop over a short list stands in for the stream. At work the events come from a log such as Kafka, which keeps them in the order they were written and hands each new one to your program as soon as it arrives.
Your task: the events, both headings and the batch job are in the starting code. Below the stream's heading, start orders and revenue at 0, then loop over events: add 1 to orders, add the event's amount to revenue, and print the event's event_time and event_id with both values.
Assignment
Follow the Coding Tutorial to keep a running count and total of the orders as each event arrives.
Hint
The print goes inside the loop, after the two updates, so each line includes the event that just arrived. The stream's last line should agree with the batch job's.
Free plan: 30 tutor messages. PRO is unlimited.
