Steps to quickly create a Kafka topic
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>3.1.0</version> </dependency>
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(); } }
mvn exec:java
kafka-topics --list --zookeeper localhost:2181
If you see my-topic
topic means that the topic is created successfully.
Note
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!