在 Debian 上创建 Kafka 主题,前提是你已经安装并启动了 Kafka(以及 Zookeeper,或使用了 KRaft 模式)。下面分步骤说明。
假设 Kafka 安装在:
/opt/kafka
# 启动 Zookeeper
/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties
# 启动 Kafka
/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/kraft/server.properties
/opt/kafka/bin/kafka-topics.sh \
--create \
--zookeeper localhost:2181 \
--replication-factor 1 \
--partitions 1 \
--topic test-topic
/opt/kafka/bin/kafka-topics.sh \
--create \
--bootstrap-server localhost:9092 \
--replication-factor 1 \
--partitions 1 \
--topic test-topic
参数说明:
--topic:主题名--partitions:分区数--replication-factor:副本数(不能大于 broker 数量)/opt/kafka/bin/kafka-topics.sh \
--list \
--bootstrap-server localhost:9092
查看主题详情:
/opt/kafka/bin/kafka-topics.sh \
--describe \
--topic test-topic \
--bootstrap-server localhost:9092
/opt/kafka/bin/kafka-console-producer.sh \
--topic test-topic \
--bootstrap-server localhost:9092
/opt/kafka/bin/kafka-console-consumer.sh \
--topic test-topic \
--from-beginning \
--bootstrap-server localhost:9092
missing required argument通常是 Kafka 版本问题:
--bootstrap-server--zookeeper报错:
replication factor: 3 larger than available brokers: 1
解决:改成 --replication-factor 1
如果你愿意,可以告诉我:
我可以给你更精确的命令。