> ## 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.

# scripts/create_topics.py

> Create the Kafka topic the project streams through.

`scripts/create_topics.py` · chapter [Replaying the data](https://calendar-effects.mintlify.site/tour/04-replaying.md), file 2 · 32 lines · [open in the repository](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/scripts/create_topics.py)

## What it does

Create the Kafka topic the project streams through.

Run on the course cluster:   python3 scripts/create\_topics.py
Run against a local Kafka:   python3 scripts/create\_topics.py --bootstrap localhost:9092

The cluster is shared by the whole class, so our topic name starts with the
project name to avoid clashing with other groups.

## Connections

**Uses:** [`src/common/contracts.py`](https://calendar-effects.mintlify.site/reference/src/common/contracts), [`src/common/settings.py`](https://calendar-effects.mintlify.site/reference/src/common/settings)

## Source

```python title="scripts/create_topics.py" lines expandable theme={null}
"""Create the Kafka topic the project streams through.

Run on the course cluster:   python3 scripts/create_topics.py
Run against a local Kafka:   python3 scripts/create_topics.py --bootstrap localhost:9092

The cluster is shared by the whole class, so our topic name starts with the
project name to avoid clashing with other groups.
"""
import argparse

from kafka.admin import KafkaAdminClient, NewTopic
from kafka.errors import TopicAlreadyExistsError

from common.contracts import KAFKA_TOPIC_PREFIX, KAFKA_TOPICS
from common.settings import KAFKA_BOOTSTRAP

parser = argparse.ArgumentParser()
parser.add_argument('--bootstrap', default=KAFKA_BOOTSTRAP, help='comma-separated Kafka servers')
parser.add_argument('--topic', default=KAFKA_TOPICS['master'])
parser.add_argument('--partitions', type=int, default=1,
                    help='1 keeps every message in one ordered log, simplest to reason about')
args = parser.parse_args()

admin = KafkaAdminClient(bootstrap_servers=args.bootstrap.split(','))
try:
    admin.create_topics([NewTopic(name=args.topic, num_partitions=args.partitions, replication_factor=1)])
    print('created topic', args.topic)
except TopicAlreadyExistsError:
    print('topic already exists:', args.topic)
print('topics on the server with our prefix:',
      sorted(t for t in admin.list_topics() if t.startswith(KAFKA_TOPIC_PREFIX)))
admin.close()
```


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