如何使用Python-kafka管理客户端在Kafka中创建主题?

时间:2018-05-01 00:51:37

标签: apache-kafka kafka-consumer-api kafka-producer-api confluent-kafka confluent

是否有任何Python kafka管理客户端可以从python程序创建主题/删除主题?我找到了一些python apis,但没有一个有Admin api可用吗?

汇合是否有python admin api?

3 个答案:

答案 0 :(得分:1)

from kafka.admin import KafkaAdminClient, 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)

答案 1 :(得分:1)

我发现了:

https://github.com/confluentinc/confluent-kafka-python

至少在创建主题时,它对我有用,我指定了一个或多个主题,并在我的kafka代理中创建了它们。

查看我自己的答案:

Could I create a topic in Kafka via API?

我问的和你差不多。

使用示例:

from confluent_kafka.admin import AdminClient, NewTopic

topic = sys.argv[1]

topics = ["newTopicExample","newTopicExample2"]

# Create topic in our Kafka, using kafka-python library.
a = AdminClient({'bootstrap.servers': 'myKafkaBrokerURL'})

new_topics = [NewTopic(topic, num_partitions=3, replication_factor=1) for topic in topics]

# Call create_topics to asynchronously create topics. A dict
# of <topic,future> is returned.
fs = a.create_topics(new_topics)

# Wait for each operation to finish.
for topic, f in fs.items():
    try:
        f.result()  # The result itself is None
        print("Topic {} created".format(topic))
    except Exception as e:
        print("Failed to create topic {}: {}".format(topic, e))

答案 2 :(得分:-1)

Confluent Python Kafka Client确实具有管理员支持。

查看此example,了解如何使用它。