Reputation: 311
is there any Python kafka admin client avilable to create topic/delete topic from python program ? I found some python apis but none of them have Admin api available ?
Does confluent have python admin api ?
Upvotes: 9
Views: 11618
Reputation: 318
I found this:
It worked for me, at least, at the topic creation, I specify one or multiple topics and it creates them in my kafka broker.
See my own answer:
I asked practically the same as you.
Example of use:
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))
Upvotes: 4
Reputation: 401
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)
Upvotes: 8
Reputation: 952
The Confluent Python Kafka Client does indeed have admin support.
Check out this example to learn how to use it.
Upvotes: -3