您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        # Kafka常用命令有哪些
Apache Kafka作为分布式流处理平台的核心组件,其命令行工具是运维和开发过程中不可或缺的部分。本文将全面介绍Kafka的常用命令,涵盖主题管理、生产者消费者操作、集群监控等场景,帮助您快速掌握Kafka命令行操作。
---
## 一、环境准备与基础命令
### 1. 启动Kafka服务
```bash
# 启动Zookeeper(Kafka 2.8+版本可不依赖Zookeeper)
bin/zookeeper-server-start.sh config/zookeeper.properties
# 启动Kafka Broker
bin/kafka-server-start.sh config/server.properties
bin/kafka-topics.sh --version
bin/kafka-run-class.sh
bin/kafka-topics.sh --create \
  --bootstrap-server localhost:9092 \
  --replication-factor 3 \
  --partitions 6 \
  --topic test-topic
bin/kafka-topics.sh --list \
  --bootstrap-server localhost:9092
bin/kafka-topics.sh --describe \
  --bootstrap-server localhost:9092 \
  --topic test-topic
# 增加分区数(不可减少)
bin/kafka-topics.sh --alter \
  --bootstrap-server localhost:9092 \
  --topic test-topic \
  --partitions 10
bin/kafka-topics.sh --delete \
  --bootstrap-server localhost:9092 \
  --topic test-topic
bin/kafka-console-producer.sh \
  --bootstrap-server localhost:9092 \
  --topic test-topic
# 从最新消息开始消费
bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic test-topic
# 从最早消息开始消费
bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic test-topic \
  --from-beginning
# 消费指定消费者组
bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic test-topic \
  --group test-group
bin/kafka-consumer-groups.sh \
  --bootstrap-server localhost:9092 \
  --list
bin/kafka-consumer-groups.sh \
  --bootstrap-server localhost:9092 \
  --describe \
  --group test-group
# 重置到最早偏移量
bin/kafka-consumer-groups.sh \
  --bootstrap-server localhost:9092 \
  --group test-group \
  --reset-offsets \
  --to-earliest \
  --execute \
  --topic test-topic
bin/kafka-broker-api-versions.sh \
  --bootstrap-server localhost:9092
bin/kafka-topics.sh --describe \
  --bootstrap-server localhost:9092 \
  --under-replicated-partitions
bin/kafka-cluster.sh \
  --bootstrap-server localhost:9092 \
  --cluster-info
# 创建ACL规则
bin/kafka-acls.sh \
  --bootstrap-server localhost:9092 \
  --add \
  --allow-principal User:Alice \
  --operation Read \
  --topic test-topic
# 修改Broker配置
bin/kafka-configs.sh \
  --bootstrap-server localhost:9092 \
  --entity-type brokers \
  --entity-name 1 \
  --alter \
  --add-config log.retention.hours=168
bin/kafka-dump-log.sh \
  --files /tmp/kafka-logs/test-topic-0/00000000000000000000.log \
  --print-data-log
| 参数 | 说明 | 
|---|---|
--bootstrap-server | 
指定Kafka集群地址 | 
--topic | 
指定操作的主题名称 | 
--partitions | 
设置分区数量 | 
--replication-factor | 
设置副本因子 | 
--from-beginning | 
从最早消息开始消费 | 
--group | 
指定消费者组 | 
--bootstrap-server替代--zookeeper参数(Kafka 2.2+版本)delete.topic.enable=true通过掌握这些常用命令,您已经能够完成Kafka日常运维的大部分操作。建议结合官方文档和实际场景灵活运用这些命令。
本文基于Kafka 3.0+版本编写,部分命令在旧版本中可能存在差异 “`
注:本文实际约1500字,通过Markdown格式呈现了Kafka的完整命令行操作指南,包含代码块、表格等结构化元素,便于阅读和实践操作。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。