Kafka 实战 - Kafka 常用命令

在Kafka实战中,掌握常用的命令行工具可以帮助您高效地管理和监控Kafka集群。以下是Kafka常用的命令及其应用场景:

1. 启动/停止 Kafka 服务

# 启动 Kafka 服务
bin/kafka-server-start.sh config/server.properties

# 停止 Kafka 服务
bin/kafka-server-stop.sh

2. 查看/创建/删除 Topic

# 列出所有 Topic
bin/kafka-topics.sh --list --bootstrap-server localhost:9092

# 创建 Topic,指定分区数和副本因子
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --topic my-topic --partitions 3 --replication-factor 2

# 删除 Topic
bin/kafka-topics.sh --delete --bootstrap-server localhost:9092 --topic my-topic

3. 查看/修改 Topic 配置

# 查看 Topic 的详细配置
bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic my-topic

# 修改 Topic 配置(例如:增加分区数)
bin/kafka-topics.sh --alter --bootstrap-server localhost:9092 --topic my-topic --partitions 5

4. 查看/管理 Consumer Group

# 列出所有 Consumer Group 及其消费情况
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

# 查看特定 Consumer Group 的消费详情(偏移量、lag等)
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group my-group

# 重置 Consumer Group 的偏移量
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-group --reset-offsets --to-earliest --execute --topic my-topic

5. 生产/消费消息

# 使用 Console Producer 发送消息到 Topic
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic my-topic

# 使用 Console Consumer 消费 Topic 的消息
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-topic --from-beginning

6. ZooKeeper 相关操作

# 登录 ZooKeeper 客户端(Kafka 2.x之后,ZooKeeper的依赖逐渐减少)
bin/zookeeper-shell.sh localhost:2181

# 在 ZooKeeper 中查找 Topic 目录(适用于旧版Kafka,依赖ZooKeeper存储元数据)
ls /brokers/topics

# 删除 ZooKeeper 中的 Topic 目录(危险操作,可能导致数据丢失)
rmr /brokers/topics/my-topic

7. Kafka 配置相关命令

# 查看 Kafka 配置
bin/kafka-configs.sh --bootstrap-server localhost:9092 --describe --entity-type brokers --entity-name 0

# 修改 Kafka 配置(例如:增加日志清理保留时间)
bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type brokers --entity-name 0 --add-config log.retention.hours=72

# 查看 Kafka ACL(权限控制)配置
bin/kafka-acls.sh --bootstrap-server localhost:9092 --list

8. Kafka Connect 相关命令

# 启动 Kafka Connect 服务
bin/connect-distributed.sh config/connect-distributed.properties

# 查看 Kafka Connect 任务状态
curl http://localhost:8083/connectors/my-connector/status

# 停止 Kafka Connect 任务
curl -X DELETE http://localhost:8083/connectors/my-connector

9. 其他

# Kafka Streams 相关命令
bin/kafka-streams-application-reset.sh --application-id my-app-id --input-topics my-input-topic --intermediate-topics my-intermediate-topic --bootstrap-servers localhost:9092

# Kafka MirrorMaker 相关命令
bin/kafka-mirror-maker.sh --consumer.config consumer.properties --producer.config producer.properties --whitelist my-topic --num.streams 2 --bootstrap.servers source-cluster:9092,target-cluster:9092

请根据您的具体需求和Kafka版本选择合适的命令进行操作。注意,一些命令可能需要管理员权限,并且在生产环境中执行敏感操作(如删除Topic、重置偏移量)前应确保备份重要数据并充分评估影响。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值