Linux的kafka命令
Linux的kafka命令详细介绍
在 Linux 环境下,Kafka 提供了一系列命令行工具(位于bin/目录下)用于集群管理、主题操作、消息生产消费等。以下是常用的 Kafka 命令分类及示例:
一、服务启动与停止
- 启动 ZooKeeper 服务(Kafka 依赖 ZooKeeper)
bash./bin/zookeeper-server-start.sh config/zookeeper.properties./bin/zookeeper-server-start.sh config/zookeeper.properties
- 启动 Kafka Broker 服务
bash./bin/kafka-server-start.sh config/server.properties./bin/kafka-server-start.sh config/server.properties
- 停止服务
bash./bin/zookeeper-server-stop.sh./bin/kafka-server-stop.sh
二、主题(Topic)管理
- 创建主题
bash./bin/kafka-topics.sh--bootstrap-server localhost:9092
--replication-factor
my-topic
- 列出所有主题
bash./bin/kafka-topics.sh --bootstrap-server localhost:9092 - 描述主题详情
bash./bin/kafka-topics.sh --bootstrap-server localhost:9092 my-topic - 删除主题(需配置
delete.topic.enable=true在server.properties中)
bash./bin/kafka-topics.sh --bootstrap-server localhost:9092 my-topic - 修改主题配置(如新增分区)
bash./bin/kafka-topics.sh --bootstrap-server localhost:9092 my-topic
三、消息生产与消费
1. 控制台生产者(发送消息)
bash
./bin/kafka-console-producer.sh--bootstrap-server localhost:9092
my-topic
输入消息后按回车发送,支持从文件读取:
bash
./bin/kafka-console-producer.sh --bootstrap-server localhost:9092 my-topic message.txt
2. 控制台消费者(消费消息)
- 从最新位置开始消费(默认)
bash./bin/kafka-console-consumer.sh--bootstrap-server localhost:9092
my-topic
my-consumer-group
- 从最早位置(earliest)开始消费
bash./bin/kafka-console-consumer.sh--bootstrap-server localhost:9092
my-topic
--from-beginning
- 指定分区和偏移量消费
bash./bin/kafka-console-consumer.sh--bootstrap-server localhost:9092
my-topic
四、消费者组(Consumer Group)管理
- 列出所有消费者组
bash./bin/kafka-consumer-groups.sh--bootstrap-server localhost:9092
- 描述消费者组状态
bash./bin/kafka-consumer-groups.sh--bootstrap-server localhost:9092
my-consumer-group
- 重置消费者组偏移量(如重置到最早或最新位置)
bash./bin/kafka-consumer-groups.sh--bootstrap-server localhost:9092
my-consumer-group
my-topic
--reset-offsets --to-earliest
五、其他实用命令
- 查看 Kafka 日志
bash/path/to/kafka/logs/server.log/path/to/kafka/logs/zookeeper.log
- 运行 Kafka 工具类(如查看集群元数据)
bash./bin/kafka-run-class.sh kafka.admin.AdminClientConfig - 镜像主题(跨集群复制)
bash./bin/kafka-mirror-maker.sh--source-connector-properties source:9092
--target-connector-properties target:9092
my-topic
注意事项
- 路径问题:若未将 Kafka 的
bin/目录加入环境变量,需进入 Kafka 安装目录执行命令(如cd /opt/kafka_2.13-3.6.0)。 - 版本差异:不同 Kafka 版本的命令参数可能略有不同,建议通过
--help查看详细用法:bash./bin/kafka-topics.sh912./bin/kafka-topics.sh
- 安全配置:若启用了 SSL/TLS 或 SASL 认证,需添加对应的认证参数(如
--consumer.config或--producer.config)。
通过以上命令,可完成 Kafka 集群的日常管理、主题操作及消息生产消费等核心功能。