Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a Kafka topic exists in Python

I want to create a Kafka topic if it does not already exist. I know how to create a topic via the bash, but I don't know how to check whether it exists.

topic_exists = ??????
if not topic_exists:
    subprocess.call([os.path.join(KAFKABIN, 'kafka-topics.sh'),
        '--create',  
        '--zookeeper', '{}:2181'.format(KAFKAHOST),
        '--topic', str(self.topic), 
        '--partitions', str(self.partitions),
        '--replication-factor', str(self.replication_factor)])
like image 866
Thomas Schreiter Avatar asked Jun 19 '15 16:06

Thomas Schreiter


1 Answers

Another nice way is with python kafka module:

kafka_client = kafka.KafkaClient(kafka_server_name)
server_topics = kafka_client.topic_partitions

if topic_name in server_topics:
   your code....

kafka_client.topic_partitions returns list of topics.

like image 86
Yonatan Kiron Avatar answered Oct 02 '22 19:10

Yonatan Kiron