I'm using the confluent-kafka-python package to interface with a Kafka server. I can successfully create the topic and push events to it. However, my problem lies when I spin up multiple nodes (running in Docker), if a second instance also tries to create the topic I get an error. I need to first check if the topic already exists before creating the new topic.
from confluent_kafka.admin import AdminClient, NewTopic
kafka_admin = AdminClient({"bootstrap.servers": server})
# First check here if the topic already exists!
if not topic_exists(topic): # <-- how to accomplish this?
new_kafka_topic = NewTopic(topic, num_partitions=1, replication_factor=1)
results = kafka_admin.create_topics([new_kafka_topic])
Thanks for any help!