在 Ubuntu 上配置 Kafka 主题(Topic),通常分为创建主题、查看、修改、删除等步骤。下面以最常见、最实用的方式说明(假设你已经安装并启动了 Kafka)。
# 启动 Zookeeper(老版本需要,Kafka 2.8+ 可不用)
bin/zookeeper-server-start.sh config/zookeeper.properties
# 启动 Kafka
bin/kafka-server-start.sh config/server.properties
如果你是用 apt / docker 安装的,命令路径可能不同,但思路一样。
bin/kafka-topics.sh \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1
说明:
test-topic:主题名partitions 3:分区数replication-factor 1:副本数(单机只能是 1)bin/kafka-topics.sh \
--create \
--topic test-topic \
--zookeeper localhost:2181 \
--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 \
--bootstrap-server localhost:9092 \
--add-config retention.ms=86400000
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
如果你用 apt 安装:
/usr/bin/kafka-topics
/usr/bin/kafka-console-producer
/usr/bin/kafka-console-consumer
如果你用 Docker:
docker exec -it kafka kafka-topics --list --bootstrap-server localhost:9092
如果你愿意,可以告诉我:
我可以给你完全匹配你环境的命令。