Kafka常用命令大全及kafka-console-consumer.sh及参数说明

简介: 该文章汇总了Kafka常用命令,包括集群管理、Topic操作、生产者与消费者的命令行工具使用方法等,适用于Kafka的日常运维和开发需求。

kafka常用命令

一、脚本简介

1、kafka-acls.sh #配置,查看kafka集群鉴权信息
2、kafka-configs.sh #查看,修改kafka配置
3、kafka-console-consumer.sh #消费命令
4、kafka-console-producer.sh #生产命令
5、kafka-consumer-groups.sh #查看消费者组,重置消费位点等
6、kafka-consumer-perf-test.sh #kafka自带消费性能测试命令
7、kafka-mirror-maker.sh #kafka集群间同步命令
8、kafka-preferred-replica-election.sh #重新选举topic分区leader
9、kafka-producer-perf-test.sh #kafka自带生产性能测试命令
10、kafka-reassign-partitions.sh #kafka数据重平衡命令
11、kafka-run-class.sh #kafka执行脚本
12、kafka-server-start.sh #进程启动
13、kafka-server-stop.sh #进程停止
14、kafka-topics.sh #查询topic状态,新建,删除,扩容

二、常用命令大全

###1、集群相关命令###
bin/zookeeper-server-start.sh config/zookeeper.properties & # 启动zookeeper
bin/zookeeper-server-stop.sh # 停止zookeeper

bin/zkServer.sh start
bin/zkServer.sh stop

bin/kafka-server-start.sh $path/server.properties # 前台启动broker
bin/kafka-server-start.sh -daemon $path/server.properties # 后台启动broker
bin/kafka-server-stop.sh # 关闭broker

###2、topic###
bin/kafka-topics.sh --create --zookeeper $zkhost --replication-factor $replCount --partitions $partitionCount --topic $topicName #指定副本数、pritition数创建topic
bin/kafka-topics.sh --zookeeper $zkhost --alter --topic $topicName --partitions $partitionCount #扩容分区、只能扩,不能缩(涉及数据迁移、合并所以不支持))
bin/kafka-topics.sh --zookeeper $zkhost --alter --config.compression.type=gzip --topic $topicName  #动态配置topic参数
    --config.compression.type=gzip #修改或添加参数配置
    --add-config.compression.type=gzip #添加参数配置
    --delete-config.compression.type=gzip #删除参数配置
bin/kafka-topics.sh --zookeeper $zkhost --delete --topic $topicName #删除topic、需关注配置文件delete.topic.enble=true,否则无法真正删除
bin/kafka-topics.sh --zookeeper $zkhost --list #查看topic列表
bin/kafka-topics.sh --zookeeper $zkhost --describe #查看所有topic详细信息
    --topic $topicname   #指定topic查看详细信息

####################topic详细信息####################
Topic:test    PartitionCount:3    ReplicationFactor:3    Configs:
    Topic: test    Partition: 0    Leader: 0    Replicas: 0,1,2    Isr: 0,2,1
    Topic: test    Partition: 1    Leader: 1    Replicas: 1,2,0    Isr: 1,2,0
    Topic: test    Partition: 2    Leader: 2    Replicas: 2,0,1    Isr: 2,0,1
    #Topic:topic名称 
    #prititionCount:partition数量
    #replicationFactor:副本数量
    #configs:其他配置
    #Partition:prititonId
    #Leader:leader节点
    #Replicas:挂载节点
    #ISR:存活节点
######################################################


###3、消费者组###
bin/kafka-consumer-groups.sh --bootstrap-server $nodes --list #消费者列表查询(待验证)
bin/kafka-consumer-groups.sh --bootstrap-server $nodes --describe --group $groupName #显示某个消费组的消费详情(待验证)
bin/kafka-consumer-groups.sh --bootstrap-server $nodes --group $groupname --reset-offsets --all-topics --to-earliest --execute # 重设消费者组位移(待验证) 
    --to-latest --execute # 最新处    
    --to-offset 2000 --execute # 某个位置
    --to-datetime 2019-09-15T00:00:00.000 # 调整到某个时间之后的最早位移
bin/kafka-consumer-groups.sh --zookeeper $zkhost --delete --group $groupname # 删除消费者组
#####消费组的消费详情#######
TOPIC        PARTITION    CURRENT-OFFSET       LOG-END-OFFSET    LAG         CONSUMER-ID    HOST    CLIENT-ID
topic名字    分区id        当前已消费的条数    总条数        未消费的条数    消费id       主机ip    客户端id
############################


###4、生产、消费者###
bin/kafka-console-producer.sh --broker-list $nodes --topic $topicName # 使用生产者
bin/kafka-console-consumer.sh --bootstrap-server $nodes --topic $topicName --from-beginning # 使用消费者
    --from-beginning #为可选参数,表示要从头消费消息
    --from-earliest #从最早的消息开始消费(待验证)
    --from-latest #从最新的消息开始消费
    --指定offset #从指定的位置开始消费
bin/kafka-console-consumer.sh --bootstrap-server $nodes --topic $topicName --from-beginning --consumer-property group.id=$groupname # 指定groupid
bin/kafka-console-consumer.sh --bootstrap-server $nodes --topic $topicName --from-beginning --partition $partitionId # 指定分区
bin/kafka-console-consumer.sh --bootstrap-server $nodes --topic $topicName --new-consumer --from-beginning --consumer.config config/consumer.properties # 新消费者
bin/kafka-simple-consumer-shell.sh --brist localhost:9092 --topic test --partition 0 --offset 1234  --max-messages 10 # 高级点的用法(不常使用,未验证)


###5、切换leader###
./bin/kafka-preferred-replica-election.sh --zookeeper 10.161.68.123:2181,10.161.68.124:2181,10.161.68.125:2181  #切换leader,此处指定一个node


###6、压测命令###
bin/kafka-producer-perf-test.sh --topic $topicName --num-records 100 --record-size 1 --throughput 100  --producer-props bootstrap.servers=$nodes #(未验证)


###7、迁移分区###
#可先修改保存时间避免大量数据同步#
bin/kafka-configs.sh --zookeeper=$zkhost --alter --entity-type topics --add-config retention.ms=86400000 --entity-name $topicName


#创建json,json格式如下#
{
   "version":1, "partitions":[
{
   "topic":"__consumer_offsets","partition":0,"replicas":[0,1]},
{
   "topic":"__consumer_offsets","partition":1,"replicas":[0,1]},
{
   "topic":"__consumer_offsets","partition":2,"replicas":[0,1]},
{
   "topic":"__consumer_offsets","partition":3,"replicas":[0,1]},
{
   "topic":"__consumer_offsets","partition":4,"replicas":[0,1]},
{
   "topic":"__consumer_offsets","partition":5,"replicas":[0,1]},
{
   "topic":"__consumer_offsets","partition":6,"replicas":[0,1]}
]}
#执行#
bin/kafka-reassign-partitions.sh --zookeeper $zkhost --reassignment-json-file $jsonFile --execute
#验证#
bin/kafka-reassign-partitions.sh --zookeeper $zkhost --reassignment-json-file $jsonFile --verify


###8、动态参数配置###
bin/kafka-configs.sh --zookeeper=$zkhost --alter --add-config '$具体参数' --entity-type $entitytype --entity-name $entityname #只可修改支持动态配置的参数,其他的报错
    #entityType常用的包括以下几个方面,根据entityType的不同前面选择zk 或者 broker
    topics 、 users、 brokers、 concumers、 groups

kafka-console-consumer.sh 详解

概述

kafka-console-consumer.sh 脚本是一个简易的消费者控制台。该 shell 脚本的功能通过调用 kafka.tools 包下的 ConsoleConsumer 类,并将提供的命令行参数全部传给该类实现。

注意:Kafka 从 2.2 版本开始将 kafka-topic.sh 脚本中的 −−zookeeper 参数标注为 “过时”,推荐使用 −−bootstrap-server 参数。若读者依旧使用的是 2.1 及以下版本,请将下述的 --bootstrap-server 参数及其值手动替换为 --zookeeper zk1:2181,zk2:2181,zk:2181。一定要注意两者参数值所指向的集群地址是不同的。

消息消费

表示从 latest 位移位置开始消费该主题的所有分区消息,即仅消费正在写入的消息。

bin/kafka-console-consumer.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --topic topicName

从开始位置消费

bin/kafka-console-consumer.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --from-beginning --topic topicName

表示从指定主题中有效的起始位移位置开始消费所有分区的消息。
显示key消费

bin/kafka-console-consumer.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --property print.key=true --topic topicName

消费出的消息结果将打印出消息体的 key 和 value。

参数说明

若还需要为你的消息添加其他属性,请参考下述列表。

参数 值类型 说明 有效值
–topic string 被消费的topic
–whitelist string 正则表达式,指定要包含以供使用的主题的白名单
–partition integer 指定分区除非指定’–offset’,否则从分区结束(latest)开始消费
–offset string 执行消费的起始offset位置默认值:latest latest earliest
–consumer-property string 将用户定义的属性以key=value的形式传递给使用者
–consumer.config string 消费者配置属性文件请注意,[consumer-property]优先于此配置
–formatter string 用于格式化kafka消息以供显示的类的名称默认值:kafka.tools.DefaultMessageFormatter
–property string 初始化消息格式化程序的属性
–from-beginning 从存在的最早消息开始,而不是从最新消息开始
–max-messages integer 消费的最大数据量,若不指定,则持续消费下去
–timeout-ms integer 在指定时间间隔内没有消息可用时退出
–skip-message-on-error 如果处理消息时出错,请跳过它而不是暂停
–bootstrap-server string 必需(除非使用旧版本的消费者),要连接的服务器
–key-deserializer string
–value-deserializer string
–enable-systest-events string 除记录消费的消息外,还记录消费者的生命周期(用于系统测试)
–isolation-level string 设置为read_committed以过滤掉未提交的事务性消息,设置为read_uncommitted以读取所有消息,默认值:read_uncommitted
–blacklist string 要从消费中排除的主题黑名单
–csv-reporter-enabled 如果设置,将启用csv metrics报告器
–delete-consumer-offsets string 如果指定,则启动时删除zookeeper中的消费者信息
–metrics-dir 输出csv度量值,需与[csv-reporter-enable]配合使用
–zookeeper string 必需(仅当使用旧的使用者时)连接zookeeper的字符串。可以给出多个URL以允许故障转移
=
相关文章
|
8天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
4天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2463 14
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
4天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1503 14
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
1月前
|
运维 Cloud Native Devops
一线实战:运维人少,我们从 0 到 1 实践 DevOps 和云原生
上海经证科技有限公司为有效推进软件项目管理和开发工作,选择了阿里云云效作为 DevOps 解决方案。通过云效,实现了从 0 开始,到现在近百个微服务、数百条流水线与应用交付的全面覆盖,有效支撑了敏捷开发流程。
19274 29
|
1月前
|
人工智能 自然语言处理 搜索推荐
阿里云Elasticsearch AI搜索实践
本文介绍了阿里云 Elasticsearch 在AI 搜索方面的技术实践与探索。
18822 20
|
1月前
|
Rust Apache 对象存储
Apache Paimon V0.9最新进展
Apache Paimon V0.9 版本即将发布,此版本带来了多项新特性并解决了关键挑战。Paimon自2022年从Flink社区诞生以来迅速成长,已成为Apache顶级项目,并广泛应用于阿里集团内外的多家企业。
17515 13
Apache Paimon V0.9最新进展
|
6天前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
365 11
|
1月前
|
存储 人工智能 前端开发
AI 网关零代码解决 AI 幻觉问题
本文主要介绍了 AI Agent 的背景,概念,探讨了 AI Agent 网关插件的使用方法,效果以及实现原理。
18697 16
|
2天前
|
算法 Java
JAVA并发编程系列(8)CountDownLatch核心原理
面试中的编程题目“模拟拼团”,我们通过使用CountDownLatch来实现多线程条件下的拼团逻辑。此外,深入解析了CountDownLatch的核心原理及其内部实现机制,特别是`await()`方法的具体工作流程。通过详细分析源码与内部结构,帮助读者更好地理解并发编程的关键概念。
|
2天前
|
SQL 监控 druid
Druid连接池学习
Druid学习笔记,使用Druid进行密码加密。参考文档:https://github.com/alibaba/druid
195 82