> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 4. Replaying the data

> Sending the rows, one trading day at a time, through Kafka or a folder of files.

The streaming job needs a stream. A **producer** reads `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 topic `calendar-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-day` sets 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.

Both can hold back a small share of messages and send them **late** (`--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

> [accordion] 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.

> [accordion] Message key
>
> The ticker. Kafka sends messages with the same key to the same partition, which keeps each stock's days in order.

> [accordion] 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

```mermaid theme={null}
%% Calendar Effects · 4 of 8 · Replaying ten years as if they were happening live.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart LR
  master[("clean/master.parquet<br/>10 years, 98,046 rows")]

  subgraph KAFKA["The real way · Kafka · Sanchi"]
    create["scripts/create_topics.py<br/>makes the topic once<br/>1 partition = one ordered line of messages"]
    producer["src/producers/replay_master.py · the PRODUCER<br/>sorts rows by date, then ticker<br/>sends each row as one JSON message, key = ticker<br/>waits --seconds-per-day between trading days, 0.1 s = about 4 min"]
    late["--late-fraction 0.02 --late-days 3<br/>holds back about 2% of messages<br/>and sends them 3 trading days late<br/>to test how the job handles late data"]
    topic[["Kafka TOPIC calendar-effects-master<br/>on the course cluster node1.local:9092, node2.local:9092<br/>a durable queue: messages stay until expired"]]
    peek["scripts/peek_topic.py<br/>prints the first few messages, to check"]
  end

  subgraph FILES["The laptop way · no Kafka needed"]
    tofiles["src/producers/replay_to_files.py<br/>same messages and the same late option,<br/>written as JSON-lines files instead"]
    folder[/"data/replay/00000.json, 00001.json ...<br/>one file per 5 trading days"/]
  end

  msg["ONE MESSAGE looks like<br/>ticker WMT, date 2024-11-29, adj_close 92.1,<br/>volume 12000000, ret 0.012, vol_ratio 1.4,<br/>views 6500.0, event_type black_friday,<br/>country US, retailer_type store<br/>empty values are sent as null"]

  job["src/streaming/job.py<br/>--source kafka reads the topic<br/>--source files reads the folder, one file per micro-batch"]

  master --> producer
  create --> topic
  late -.-> producer
  producer --> topic
  topic --> peek
  topic --> job
  master --> tofiles --> folder --> job
  producer -.-> msg
```

The [full diagram and its guide](https://calendar-effects.mintlify.site/diagrams/04-kafka-replay.md) 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](https://calendar-effects.mintlify.site/reference/src/producers/replay_to_files.md)

**2. scripts/create\_topics.py**

Create the Kafka topic the project streams through. [Read it](https://calendar-effects.mintlify.site/reference/scripts/create_topics.md)

**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](https://calendar-effects.mintlify.site/reference/src/producers/replay_master.md)

**4. scripts/peek\_topic.py**

Print the first few messages in our Kafka topic, to check the producer works. [Read it](https://calendar-effects.mintlify.site/reference/scripts/peek_topic.md)

## Questions you should be able to answer

> [accordion] 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.

> [accordion] 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.

> [accordion] 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.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.