Home > Java > javaTutorial > body text

Commonly used Kafka topic creation commands

王林
Release: 2024-02-01 08:04:23
Original
858 people have browsed it

Commonly used Kafka topic creation commands

1. Create a topic using the command line

kafka-topics --create --topic test --partitions 3 --replication-factor 2
Copy after login

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));
Copy after login

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)
Copy after login

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
Copy after login

This command will create a topic named "test" with 3 partitions and 2 replication factors .

5. Use Kafka UI to create a topic

  1. Open the Kafka UI at http://localhost:8083.
  2. Click on the "Themes" tab.
  3. Click the "Create Theme" button.
  4. In the Name field, enter a name for the topic.
  5. In the "Number of partitions" field, enter the number of partitions.
  6. In the "Replication Factor" field, enter the replication factor.
  7. Click the "Create" button.

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:

  • Use [Kafka REST Proxy] (https://kafka.apache.org/documentation/#rest_proxy)
  • Use [Kafka Python Package](https://github.com/dpkp/kafka-python)
  • Use [Kafka Go Package](https://github.com/Shopify/sarama)
  • Use [Kafka Node.js Package](https://github.com/segmentio/kafka-node)

Notes

  • When creating a theme, you need to make sure that the theme name is unique.
  • You must also ensure that the number of partitions and replication factor are compatible with your application needs.
  • If you create a topic with a large number of partitions, you may experience performance issues.
  • If you create a topic with a large replication factor, this may increase storage overhead.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template