欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何使用python编程创建kafka的topic

程序员文章站 2022-03-07 20:33:01
...

You can programmatically create topics either using kafka-python or confluent_kafka client which is a lightweight wrapper around librdkafka.

Using kafka-python

from kafka.admin importKafkaAdminClient,NewTopic
admin_client =KafkaAdminClient(bootstrap_servers="localhost:9092", client_id='test')

topic_list =[]
topic_list.append(NewTopic(name="example_topic", num_partitions=1, replication_factor=1))
admin_client.create_topics(new_topics=topic_list, validate_only=False)

Using confluent_kafka

from confluent_kafka.admin importAdminClient,NewTopic
admin_client =AdminClient({"bootstrap_servers":"localhost:9092"})

topic_list =[]
topic_list.append(NewTopic("example_topic",1,1))
admin_client.create_topics(topic_list)
 
相关标签: python kafka