Steps to quickly create a Kafka topic
- Import dependencies
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>3.1.0</version> </dependency>
- Create Kafka AdminClient
import org.apache.kafka.clients.admin.AdminClient; import org.apache.kafka.clients.admin.NewTopic; import java.util.Collections; import java.util.Properties; public class CreateTopic { public static void main(String[] args) { // 创建Properties对象,并设置Kafka集群的地址 Properties properties = new Properties(); properties.put("bootstrap.servers", "localhost:9092"); // 创建AdminClient对象 AdminClient adminClient = AdminClient.create(properties); // 创建NewTopic对象,并设置主题名称和分区数 NewTopic newTopic = new NewTopic("my-topic", 3); // 创建主题 adminClient.createTopics(Collections.singletonList(newTopic)); // 关闭AdminClient对象 adminClient.close(); } }
- Run code
mvn exec:java
- Verify whether the topic is created successfully
kafka-topics --list --zookeeper localhost:2181
If you see my-topic
topic means that the topic is created successfully.
Note
- When creating a topic, you need to specify the topic name and number of partitions. The number of partitions determines how many messages a topic can process simultaneously.
- When creating a topic, you can also specify other parameters, such as the number of copies, compression type, etc.
- After creating a topic, you can send messages to the topic.
- When consuming messages, you can specify a consumer group. The consumer group determines which consumers can consume the messages in the topic.
The above is the detailed content of Quick way to create a Kafka topic. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)