kafka常用命令
1)、创建topic:
./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
2)、查看topic:
./bin/kafka-topics.sh --list --zookeeper localhost:2181
3)、生产者
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
4)、消费者
./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic
test --from-beginning
5)、删除topic
./bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic test
备注:[server.properties需要 设置delete.topic.enable=true]
#列出所有group-id
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
#查询group-id的消费情况
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group course-cell-dinc-test
#删除指定的group-id
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --delete --group course-cell-dinc-test
重置group的消费记录
当选择重置消费记录操作时,目标Group的状态一定不能是活跃的。也就是该group中不能有consumer在消费。
通过 --reset-offsets 可以重置指定group的消费记录。和–reset-offsets搭配的有两个选项,–dry-run和–execute,默认是–dry-run。
dry-run 模式
当运行在–dry-run模式下,重置操作不会真正的执行,只会预演重置offset的结果。
该模式也是为了让用户谨慎的操作,否则直接重置消费记录会造成各个consumer消息读取的异常。
#--shift-by -1 表示将消费的offset重置成当前消费的offset-1
kafka-consumer-groups --bootstrap-server localhost:9092 --reset-offsets --shift-by -1 --topic global-biz-log --group course-cell-dinc-test --dry-run
输出
TOPIC PARTITION NEW-OFFSET
test 0 797054
此时如果去查询该group的消费offset,会发现该group的消费offset其实还是797055,并没有发生改变。
—execute 模式
通过–execute参数可以直接执行重置操作。
kafka-consumer-groups --bootstrap-server localhost:9092 --reset-offsets --shift-by -1 --topic global-biz-log --group course-cell-dinc-test --execute
重置offset的几种策略
该命令提供了多种offset重置策略给我们选择如何重置offset
--to-current 直接重置offset到当前的offset,也就是LOE
--to-datetime <String: datetime> 重置offset到指定时间的offset处
--to-earliest 重置offset到最开始的那条offset
--to-offset <Long: offset> 重置offset到目标的offset
--shift-by <Long:n> 根据当前的offset进行重置,n可以是正负数
--from-file <String: path to CSV file> 通过外部的csv文件描述来进行重置
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --reset-offsets --to-earliest --topic global-biz-log --group debug-01 --dry-run
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --reset-offsets --to-earliest --topic global-biz-log --group debug-01 --execute
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --reset-offsets --to-datetime 2019-11-22T06:00:00.000 --topic global-biz-log --group debug-01 --execute
使用kafka-verifiable-consumer批量拉取消息
kafka-verifiable-consumer可以批量的拉取消息,其实和kafka-console-consumer命令差不多。
不过使用kafka-verifiable-consumer消费消息输出的内容更丰富,还包括offset等信息,并且可以设置只读取几条消息等。
kafka-console-consumer是有多少读多少。
#–max-messages 5 表示只拉取5条
#–verbose 表示输出每一条消息的内容
./kafka-verifiable-consumer.sh --broker-list localhost:9092 --max-messages 5 --group-id course-cell-dinc-test --topic global-biz-log --verbose
./kafka-verifiable-consumer.sh --broker-list localhost:9092 --max-messages 1 --reset-policy earliest --group-id course-cell-dinc-test --topic global-biz-log --v
kafka-verifiable-consumer命令还支持以下参数:
--session-timeout consumer的超时时间
--enable-autocommit 是否开启自动offset提交,默认是false
--reset-policy 当以前没有消费记录时,选择要拉取offset的策略,可以是'earliest', 'latest','none'。默认是earliest
--assignment-strategy consumer分配分区策略,默认是RoundRobinAssignor
--consumer.config 指定consumer的配置
参考链接
https://blog.youkuaiyun.com/u013332124/article/details/84898927
https://blog.youkuaiyun.com/u013332124/article/details/84928186