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

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 
 

相关文章:

  • Linux:理解库制作与原理
  • 卡西欧模拟器:Windows端功能强大的计算器
  • day027-Shell自动化编程-基础
  • 如何安全高效的文件管理?文件管理方法
  • 109页PPT华为流程模块L1-L4级梳理及研发采购服务资产5级建模
  • 网页前端开发(基础进阶3--Vue)
  • 华为ICT和AI智能应用
  • 区域徘徊检测算法AI智能分析网关V4助力公共场所/工厂等多场景安全升级
  • 华为云Flexus+DeepSeek征文|华为云Flexus服务器dify平台通过自然语言转sql并执行实现电商数据分析
  • Vue前端篇——Vue 3的watch深度解析
  • 2025年想冲网安方向,该考华为安全HCIE还是CISSP?
  • SAP学习笔记 - 开发22 - 前端Fiori开发 数据绑定(Jason),Data Types(数据类型)
  • 华为手机开机卡在Huawei界面不动怎么办?
  • Vue3中Ant-design-vue的使用-附完整代码
  • 【C++】string类的模拟实现(详解)
  • 网络编程之TCP编程
  • 动态能力赋能:凯禾瑞华家务服务虚拟仿真实训室创新建设方案
  • centos 9/ubuntu 一次性的定时关机
  • CentOS7 + JDK8 虚拟机安装与 Hadoop + Spark 集群搭建实践
  • ubuntu 常用操作指令(与域控制器交互相关)
  • 深圳营销型网站建设公司选择哪家好/谷歌seo视频教程
  • 北京燕华工程建设有限公司网站/发外链的平台有哪些
  • h5网站后台管理模板/中国突然宣布一重磅消息
  • 网站规划建设与管理维护的论文/网络宣传平台有哪些
  • 视频网站做app还是h5/兰州seo优化公司
  • 用帝国做的网站只收录首页/百度手机助手下载2021新版