3.【kafka运维】Topic的生产和消费运维脚本

简介: 3.【kafka运维】Topic的生产和消费运维脚本

文章目录

1.Topic的发送kafka-console-producer.sh

2. Topic的消费kafka-console-consumer.sh

3. 持续批量推送消息kafka-verifiable-producer.sh

4. 持续批量拉取消息kafka-verifiable-consumer

More

日常运维 、问题排查 怎么能够少了滴滴开源的

滴滴开源LogiKM一站式Kafka监控与管控平台


1.Topic的发送kafka-console-producer.sh

1.1 生产无key消息

## 生产者
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test --producer.config config/producer.properties

1.2 生产有key消息

加上属性--property parse.key=true

## 生产者
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test --producer.config config/producer.properties  --property parse.key=true


默认消息key与消息value间使用“Tab键”进行分隔,所以消息key以及value中切勿使用转义字符(\t)


可选参数


参数 值类型 说明 有效值

–bootstrap-server String 要连接的服务器必需(除非指定–broker-list) 如:host1:prot1,host2:prot2

–topic String (必需)接收消息的主题名称

–batch-size Integer 单个批处理中发送的消息数 200(默认值)

–compression-codec String 压缩编解码器 none、gzip(默认值)snappy、lz4、zstd

–max-block-ms Long 在发送请求期间,生产者将阻止的最长时间 60000(默认值)

–max-memory-bytes Long 生产者用来缓冲等待发送到服务器的总内存 33554432(默认值)

–max-partition-memory-bytes Long 为分区分配的缓冲区大小 16384

–message-send-max-retries Integer 最大的重试发送次数 3

–metadata-expiry-ms Long 强制更新元数据的时间阈值(ms) 300000

–producer-property String 将自定义属性传递给生成器的机制 如:key=value

–producer.config String 生产者配置属性文件[–producer-property]优先于此配置 配置文件完整路径

–property String 自定义消息读取器 parse.key=true/false key.separator=<key.separator>ignore.error=true/false

–request-required-acks String 生产者请求的确认方式 0、1(默认值)、all

–request-timeout-ms Integer 生产者请求的确认超时时间 1500(默认值)

–retry-backoff-ms Integer 生产者重试前,刷新元数据的等待时间阈值 100(默认值)

–socket-buffer-size Integer TCP接收缓冲大小 102400(默认值)

–timeout Integer 消息排队异步等待处理的时间阈值 1000(默认值)

–sync 同步发送消息  

–version 显示 Kafka 版本 不配合其他参数时,显示为本地Kafka版本

–help 打印帮助信息  

2. Topic的消费kafka-console-consumer.sh

1. 新客户端从头消费--from-beginning (注意这里是新客户端,如果之前已经消费过了是不会从头消费的)

下面没有指定客户端名称,所以每次执行都是新客户端都会从头消费


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


2. 正则表达式匹配topic进行消费--whitelist

消费所有的topic


sh bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --whitelist ‘.*’


消费所有的topic,并且还从头消费


sh bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --whitelist ‘.*’ --from-beginning


3.显示key进行消费--property print.key=true


sh bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --property print.key=true


4. 指定分区消费--partition 指定起始偏移量消费--offset


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


5. 给客户端命名--group


注意给客户端命名之后,如果之前有过消费,那么--from-beginning就不会再从头消费了


sh bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --group test-group


6. 添加客户端属性--consumer-property


这个参数也可以给客户端添加属性,但是注意 不能多个地方配置同一个属性,他们是互斥的;比如在下面的基础上还加上属性--group test-group 那肯定不行


sh bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --consumer-property group.id=test-consumer-group


7. 添加客户端属性--consumer.config


跟--consumer-property 一样的性质,都是添加客户端的属性,不过这里是指定一个文件,把属性写在文件里面, --consumer-property 的优先级大于 --consumer.config


sh bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --consumer.config config/consumer.properties


参数 描述 例子

--group 指定消费者所属组的ID

--topic 被消费的topic

--partition 指定分区 ;除非指定–offset,否则从分区结束(latest)开始消费 --partition 0

--offset 执行消费的起始offset位置 ;默认值: latest; /latest /earliest /偏移量 --offset 10

--whitelist 正则表达式匹配topic;--topic就不用指定了; 匹配到的所有topic都会消费; 当然用了这个参数,--partition --offset等就不能使用了

--consumer-property 将用户定义的属性以key=value的形式传递给使用者 --consumer-propertygroup.id=test-consumer-group

--consumer.config 消费者配置属性文件请注意,[consumer-property]优先于此配置 --consumer.config config/consumer.properties

--property 初始化消息格式化程序的属性 print.timestamp=true,false 、print.key=true,false 、print.value=true,false 、key.separator=<key.separator> 、line.separator=<line.separator>、key.deserializer=<key.deserializer>、value.deserializer=<value.deserializer>

--from-beginning 从存在的最早消息开始,而不是从最新消息开始,注意如果配置了客户端名称并且之前消费过,那就不会从头消费了

--max-messages 消费的最大数据量,若不指定,则持续消费下去 --max-messages 100

--skip-message-on-error 如果处理消息时出错,请跳过它而不是暂停

--isolation-level 设置为read_committed以过滤掉未提交的事务性消息,设置为read_uncommitted以读取所有消息,默认值:read_uncommitted

--formatter kafka.tools.DefaultMessageFormatter、kafka.tools.LoggingMessageFormatter、kafka.tools.NoOpMessageFormatter、kafka.tools.ChecksumMessageFormatter

3. 持续批量推送消息kafka-verifiable-producer.sh

单次发送100条消息--max-messages 100


一共要推送多少条,默认为-1,-1表示一直推送到进程关闭位置


sh bin/kafka-verifiable-producer.sh --topic test_create_topic4 --bootstrap-server localhost:9092 --max-messages 100


每秒发送最大吞吐量不超过消息 --throughput 100


推送消息时的吞吐量,单位messages/sec。默认为-1,表示没有限制


sh bin/kafka-verifiable-producer.sh --topic test_create_topic4 --bootstrap-server localhost:9092 --throughput 100


发送的消息体带前缀--value-prefix


sh bin/kafka-verifiable-producer.sh --topic test_create_topic4 --bootstrap-server localhost:9092 --value-prefix 666


注意--value-prefix 666必须是整数,发送的消息体的格式是加上一个 点号. 例如: 666.


其他参数:

--producer.config CONFIG_FILE 指定producer的配置文件

--acks ACKS 每次推送消息的ack值,默认是-1


4. 持续批量拉取消息kafka-verifiable-consumer

持续消费


sh bin/kafka-verifiable-consumer.sh --group-id test_consumer --bootstrap-server localhost:9092 --topic test_create_topic4


单次最大消费10条消息--max-messages 10


sh bin/kafka-verifiable-consumer.sh --group-id test_consumer --bootstrap-server localhost:9092 --topic test_create_topic4 --max-messages 10


相关可选参数


参数 描述 例子

--bootstrap-server 指定kafka服务 指定连接到的kafka服务; –bootstrap-server localhost:9092

--topic 指定消费的topic

--group-id 消费者id;不指定的话每次都是新的组id

group-instance-id 消费组实例ID,唯一值

--max-messages 单次最大消费的消息数量

--enable-autocommit 是否开启offset自动提交;默认为false

--reset-policy 当以前没有消费记录时,选择要拉取offset的策略,可以是earliest, latest,none。默认是earliest

--assignment-strategy consumer分配分区策略,默认是org.apache.kafka.clients.consumer.RangeAssignor

--consumer.config 指定consumer的配置文件

More

Kafka专栏持续更新中…(源码、原理、实战、运维、视频、面试视频)


相关文章
|
5月前
|
运维 Shell
运维(15)-shell脚本的调试方法
运维(15)-shell脚本的调试方法
37 0
|
7月前
|
缓存 运维 Linux
Linux(CentOS)运维脚本工具集合
Linux(CentOS)运维脚本工具集合
148 2
|
28天前
|
运维 监控 Linux
linux脚本自动化运维任务
Linux自动化运维通过脚本提升效率,涵盖服务管理(启停服务、异常恢复)、系统监控(资源警报)、日志管理(清理分析)、备份恢复、补丁更新、自动化部署(如Ansible)、网络管理、定时任务(cron)和故障排查。结合shell、Python及工具,形成高效运维体系。
22 3
|
4月前
|
Linux Shell 索引
Python自动化脚本-运维人员宝典第一章 Python脚本概述
在学习本书前,你应该了解一些 Python 编程的基础知识,比如基础语法、变量类型、元组数据类型、列表字典、函数、字符串和方法。在python.org/downloads/上有3.7.2和2.7.15两个版本可供下载。本书中我们将使用3.7这一版本来作为代表示例和包的安装。
236 11
|
7月前
|
运维 关系型数据库 MySQL
Linux实用运维脚本分享
Linux实用运维脚本分享
|
2月前
|
消息中间件 Java Kafka
【Kafka】Kafka-Server-start.sh 启动脚本分析(Ver 2.7.2)
【Kafka】Kafka-Server-start.sh 启动脚本分析(Ver 2.7.2)
33 0
|
3月前
|
运维 Linux 网络安全
利用群晖NAS+shell脚本实现运维命令执行结果文件自动上传
利用群晖NAS+shell脚本实现运维命令执行结果文件自动上传
124 0
|
3月前
|
消息中间件 缓存 监控
Kafka - 3.x 消费者 生产经验不完全指北
Kafka - 3.x 消费者 生产经验不完全指北
33 0
|
4月前
|
缓存 运维 Linux
自动化运维脚本集合
自动化运维脚本集合
62 0
|
4月前
|
消息中间件 存储 缓存
分布式实时消息队列Kafka(三)生产分区规则
分布式实时消息队列Kafka(三)生产分区规则
37 0
分布式实时消息队列Kafka(三)生产分区规则