在 CentOS(Linux) 上创建 Kafka 主题(Topic),一般步骤如下(假设你已经安装并启动了 Kafka)。
# 启动 Zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties &
# 启动 Kafka
bin/kafka-server-start.sh config/server.properties &
Kafka 2.8+ 可使用 KRaft 模式,不依赖 Zookeeper
cd /opt/kafka # 根据你的安装路径
bin/kafka-topics.sh \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1
参数说明:
--topic:主题名--partitions:分区数--replication-factor:副本数(不能大于 broker 数量)--bootstrap-server:Kafka 地址bin/kafka-topics.sh \
--create \
--zookeeper localhost:2181 \
--topic test-topic \
--partitions 3 \
--replication-factor 1
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
查看主题详情:
bin/kafka-topics.sh --describe --topic test-topic --bootstrap-server localhost:9092
bin/kafka-topics.sh --delete --topic test-topic --bootstrap-server localhost:9092
需要在
server.properties中开启:
delete.topic.enable=true
bin/kafka-console-producer.sh \
--topic test-topic \
--bootstrap-server localhost:9092
bin/kafka-console-consumer.sh \
--topic test-topic \
--from-beginning \
--bootstrap-server localhost:9092
❌ replication factor: 1 larger than available brokers: 0
→ Kafka 没启动或 broker 数量不足
❌ 主题创建成功但消费不到
→ 检查 --from-beginning 或 group id
如果你用的是:
可以告诉我,我可以给你对应命令。