1. Create a topic using the command line
kafka-topics --create --topic test --partitions 3 --replication-factor 2
This command will create a topic named "test" with 3 Partitions and a replication factor of 2. This means the data will be replicated 3 times on 2 different brokers to ensure redundancy and high availability.
2. Create a topic using Java API
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); AdminClient adminClient = AdminClient.create(props); NewTopic topic = new NewTopic("test", 3, (short) 2); adminClient.createTopics(Arrays.asList(topic));
This code will create a topic named "test" with 3 partitions and 2 replication factors .
3. Create a topic using Python API
from kafka import KafkaAdminClient admin_client = KafkaAdminClient(bootstrap_servers="localhost:9092") topic_list = [ kafka.admin.NewTopic(name="test", num_partitions=3, replication_factor=2) ] admin_client.create_topics(new_topics=topic_list, validate_only=False)
This code will create a topic named "test" with 3 partitions and 2 replication factors .
4. Create a topic using REST API
curl -X POST -H "Content-Type: application/json" -d '{"name": "test", "partitions": 3, "replication_factor": 2}' http://localhost:8083/topics
This command will create a topic named "test" with 3 partitions and 2 replication factors .
5. Use Kafka UI to create a topic
The topic will be created and you will see it in the topic list.
Other options
In addition to the above methods, you can also create a topic using the following options:
Notes
The above is the detailed content of Commonly used Kafka topic creation commands. For more information, please follow other related articles on the PHP Chinese website!