clean/master.parquet and sends it, one trading day at a time, as if the past ten years were happening now.
The big idea
Each row becomes one JSON message, keyed by ticker, on the topiccalendar-effects-master. There are two ways to replay:
- Kafka (
replay_master.py): the real thing, on the course cluster or a local Kafka.--seconds-per-daysets the pace. - Files (
replay_to_files.py): the same messages written into JSON-lines files, a few trading days per file. The job reads one file per micro-batch, so it behaves like Kafka on a laptop with nothing else installed.
--late-fraction 0.02 --late-days 3), randomly but repeatably (--seed). That tests the job’s handling of late and out-of-order data, which real streams always have.
Words you will meet
Topic, partition, offset
Topic, partition, offset
A topic is a named log of messages. It can be split into partitions (we use one, so order is kept). Each message has an offset, its position in the log.
Message key
Message key
The ticker. Kafka sends messages with the same key to the same partition, which keeps each stock’s days in order.
Event time
Event time
The time a message is about (the trading day), as opposed to the time it arrives. The job works in event time, so a late message still lands on its own day.
The picture: Replaying ten years as if they were happening live
The full diagram and its guide explain every box.Read these files in this order
1
src/producers/replay_to_files.py
Replay clean/master.parquet into a folder of JSON-lines files, for running the streaming job on a laptop without Kafka. Read it
2
scripts/create_topics.py
Create the Kafka topic the project streams through. Read it
3
src/producers/replay_master.py
Replay clean/master.parquet into Kafka, one trading day at a time, as if the past ten years were happening live. Read it
4
scripts/peek_topic.py
Print the first few messages in our Kafka topic, to check the producer works. Read it
Questions you should be able to answer
Why prefix the topic with the project name?
Why prefix the topic with the project name?
The course Kafka is shared by the whole class.
calendar-effects- keeps our topic from clashing with another group’s.How do you know the producer works?
How do you know the producer works?
scripts/peek_topic.py prints the first messages in the topic, so you can see the JSON before starting Spark.What does a late message test?
What does a late message test?
That the job waits for the watermark before using a day, so a message that arrives up to 7 days late still gives exactly the same results as an in-order replay.
Ideas to improve it
- Replay several tickers’ partitions in parallel and show the job scaling with more partitions.
- Use Avro with a schema registry instead of JSON, so the contract is enforced on the wire.