当前位置: 首页 > ops >正文

kafka命令

kafka安装先安装zookeeper,jdk
确保jdk版本与kafka版本匹配:
 

先启动zookeeper:

    # 启动独立安装的zookeeper
    ./zkServer.sh start
    
    # 也可以自动kafka自带的zookerper
    ./zookeeper-server-start.sh ../config/zookeeper.properties

再启动kafka:
    cd /usr/local/kafka_2.12-3.1.0/bin
    
    ./kafka-server-start.sh -daemon ../config/server.properties

关闭kafka:
    # 关闭kafka
    ./kafka-server-stop.sh
    
    # 关闭zookeeper
    ./zkServer.sh stop

生产者:

创建一个topic:test20250604
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test20250604

查看创建的topic列表:

bin/kafka-topics.sh --bootstrap-server localhost:9092 --list

查询topic的详细信息:
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test_kafka_topic
说明:如果未指定 topic 则输出所有 topic 的信息

增加topic的partition数:

bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test_kafka_topic --partitions 5 
 

查看 topic 指定分区 offset 的最大值或最小值

time 为 -1 时表示最大值,为 -2 时表示最小值:

bin/kafka-run-class.sh kafka.tools.GetOffsetShell --topic test_kafka_topic --time -1 --broker-list 127.0.0.1:9092 --partitions 0 

删除topic:

删除名为 test_kafka_topic 的 Topic

bin/kafka-topics.sh --delete --zookeeper localhost:2181  --topic test_kafka_topic
说明:在${KAFKA_HOME}/config/server.properties中配置 delete.topic.enable 为 true,这样才能生效,删除指定的 topic主题


消费者:

消费消息(从头开始)

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test20250604 --from-beginning

--from-beginning为限制消费从头开始消费

    --from-beginning #为可选参数,表示要从头消费消息
    --from-earliest #从最早的消息开始消费(待验证)
    --from-latest #从最新的消息开始消费
    --指定offset #从指定的位置开始消费

消费消息(从尾开始)

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic --offset latest
 

消费消息(从尾开始制定分区)

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic --offset latest --partition 0
 

消费消息(指定分区和偏移量)

–partition 指定起始偏移量消费–offset:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic  --partition 0 --offset 100 
 

消费者消费消息(指定分组)
注意给客户端命名之后,如果之前有过消费,那么–from-beginning就不会再从头消费了

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092  --from-beginning --topic test_kafka_topic --group t1
 

取指定个数
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic --offset latest --partition 0 --max-messages 1
 

消费者group:

指定分组从头开始消费消息(应该会指定偏移量)

/usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test -group test_group --from-beginning

消费者group列表:
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
 

查看group详情:

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --describe

输出日志:

Consumer group 'test_group' has no active members.

TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test            0          5               5               0               -               -               -

# CURRENT-OFFSET: 当前消费者群组最近提交的 offset,也就是消费者分区里读取的当前位置
# LOG-END-OFFSET: 当前最高水位偏移量,也就是最近一个读取消息的偏移量,同时也是最近一个提交到集群的偏移量
# LAG:消费者的 CURRENT-OFFSET 与 broker 的 LOG-END-OFFSET 之间的差距
 

删除group中topic:
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --topic test --delete

删除group:
/usr/local/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --delete
 

补充:
平衡leader:
bin/kafka-preferred-replica-election.sh --bootstrap-server localhost:9092
 

自带压测工具:

bin/kafka-producer-perf-test.sh --topic test --num-records 100 --record-size 1 --throughput 100 --producer-props bootstrap.servers=localhost:9092 
 

http://www.xdnf.cn/news/11944.html

相关文章:

  • 【unity游戏开发入门到精通——通用篇】AssetBundle(AB包)和AssetBundleBrowser的使用介绍
  • 数据结构期末PTA选择汇总
  • L1-019 谁先倒 (15 分)
  • [安卓/ios辅助工具]给按键精灵脚本做一个日志悬浮窗
  • 制造业数字化转型解决方案及应用
  • rk3588开发板实现磁盘自动挂载vmware共享文件夹设置
  • C学习--内存管理
  • 2. 库的操作
  • 企业如何抵御复杂网络攻击?
  • Delphi用if else实现 select case、switch语句功能,实现case 以字符串为分类条件。
  • Java对象创建过程
  • 操作系统入门:核心概念与设计逻辑
  • 数字孪生在智能制造中的实践:某汽车总装车间的全流程仿真优化
  • https和http有什么区别-http各个版本有什么区别
  • DINO-R1
  • 商务合同范本智能审核系统 AI 大模型处理方案
  • 探索分布式存储与通信:去中心化共享及通訊(DSAC)
  • 区块链跨链通信:使用 Cosmos SDK 实现链间互操作
  • 手动清理C盘文件的一些方法
  • 共聚焦显微镜—赋能光学元件精密质控
  • C语言获取数组长度方法大全(附带实例)
  • gateway 网关 路由新增 (已亲测)
  • Python训练营打卡 Day44
  • linux shell脚本硬件定时检测通过邮箱警告管理人员
  • LLM之RAG实战(五十四)| 复杂文档处理RAG框架:Ragflow
  • 振动力学:弹性杆的纵向振动(固有振动和固有频率的概念)
  • [蓝桥杯]填字母游戏
  • 短视频矩阵系统源码新发布技术方案有那几种?
  • 2025.6.4总结
  • 霍夫曼编码详解