在 CentOS 上设置(创建/配置)Kafka 主题,通常是通过 Kafka 自带的命令行工具来完成的。下面按常见场景给你说明。
/opt/kafka 或 /usr/local/kafka)# 先启动 ZooKeeper(老版本)
bin/zookeeper-server-start.sh config/zookeeper.properties
# 再启动 Kafka
bin/kafka-server-start.sh config/server.properties
bin/kafka-topics.sh \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1
说明:
--topic:主题名--partitions:分区数--replication-factor:副本数(不能大于 broker 数量)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 \
--alter \
--topic test-topic \
--partitions 5 \
--bootstrap-server localhost:9092
bin/kafka-configs.sh \
--alter \
--topic test-topic \
--add-config retention.ms=86400000 \
--bootstrap-server localhost:9092
bin/kafka-topics.sh \
--delete \
--topic test-topic \
--bootstrap-server localhost:9092
⚠️ 需确保:
delete.topic.enable=true
(在 server.properties 中)
# 生产消息
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
cd 到 Kafka 目录如果你愿意,可以告诉我:
我可以给你更精确的命令。