So far I haven't seen a python client that implements the creation of a topic explicitly without using the configuration option to create automatically the topics.
kafka-python is designed to function much like the official java client, with a sprinkling of pythonic interfaces (e.g., consumer iterators). kafka-python is best used with newer brokers (0.9+), but is backwards-compatible with older versions (to 0.8. 0). Some features will only be enabled on newer brokers.
Here are the simple 3 steps used to Create an Apache Kafka Topic: Step 1: Setting up the Apache Kafka Environment. Step 2: Creating and Configuring Apache Kafka Topics. Step 3: Send and Receive Messages using Apache Kafka Topics.
Kafka provides two mechanisms for creating topics automatically. You can enable automatic topic creation for the Kafka broker, and, beginning with Kafka 2.6. 0, you can also enable Kafka Connect to create topics.
You can programmatically create topics using either kafka-python
or confluent_kafka
client which is a lightweight wrapper around librdkafka.
Using kafka-python
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)
Using confluent_kafka
from confluent_kafka.admin import AdminClient, NewTopic admin_client = AdminClient({ "bootstrap.servers": "localhost:9092" }) topic_list = [] topic_list.append(NewTopic("example_topic", 1, 1)) admin_client.create_topics(topic_list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With