Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create a topic in Apache Kafka using Python

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.

like image 599
jpgerek Avatar asked Sep 24 '14 16:09

jpgerek


People also ask

Can we write Kafka code in python?

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.

How do you create a topic on Kafka?

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.

Does Kafka create topics automatically?

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.


1 Answers

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) 
like image 64
Giorgos Myrianthous Avatar answered Sep 29 '22 16:09

Giorgos Myrianthous