kafka 常用命令

kafka介绍

kafka官网:http://kafka.apache.org/

各个名词解释:
*producer:发布消息的对象称之为消息生产者(Kafka topic producer)
*topic:Kafka将消息分门别类,每一类的消息称之为一个主题(Topic)
*consumer:订阅消息并处理发布的消息的对象称之为主题消费者(consumers)
*broker:已发布的消息保存在一组服务器中,称之为Kafka集群。集群中的每一个服务器都是一个代理(Broker)。 消费者可以订阅一个或多个主题(topic),并从Broker拉数据,从而消费这些已发布的消息。

消息中间件 对比

选择建议
消息中间件 建议
Kafka 追求高吞吐量,适合产生大量数据的互联网服务的数据收集业务
RocketMQ 可靠性要求很高的金融互联网领域,稳定性搞,尽力了多次阿里双11考验
RabbitMQ 性能较好,社区活跃度高,数据量没有那么大,优先选择功能比较晚上的RabbitMQ

常用命令介绍

启动kafka服务
bin/kafka-server-start.sh config/server.properties &
停止kafka服务
bin/kafka-server-stop.sh
创建一个叫demo-topic的主题(topic),有两个分区,每个分区3个副本,同时指定该主题的消息保留时长(72小时)
bin/kafka-topics.sh --zookeeper(host:port) --create --topic demo-topic --replication-factor 3 --partitions 2 --config retention.ms=259200000
列出指定主题(topic)的详细信息
bin/kafka-topics.sh  --zookeeper(host:port) --describe  --topic demo-topic
查看所有的主题
bin/kafka-topics.sh --list --zookeeper(host:port) kafka-host(host:port)
查看所有主题的详细信息
bin/kafka-topics.sh --zookeeper(host:port) --describe
删除一个主题
bin/kafka-topics.sh --zookeeper(host:port) --topic demo-topic --delete
向kafka指定topi写入数据
bin/kafka-console-producer.sh --broker-list kafka-host(host:port)--topic demo-topic
命令行消费某个topic消息
#加了--from-beginning 从头消费所有消息
bin/kafka-console-consumer.sh --bootstrap-server kafka-host(host:port) --topic demo-topic --from-beginning   

#不加--from-beginning 从最新的一条消息开始消费
bin/kafka-console-consumer.sh --bootstrap-server kafka-host(host:port) --topic demo-topic
查看某个topic对应的消息数量
# time为-1时表示最大值,time为-2时表示最小值 --partitions num 指定分区
bin/kafka-run-class.sh  kafka.tools.GetOffsetShell --broker-list kafka-host(host:port) --topic demo-topic --time -1
kafka重置分组已经消费的偏移量offest
bin/kafka-consumer-groups.sh --bootstrap-server=kafka-host(host:port) --execute --reset-offsets --topic=demo-topic --group=testPlatform --to-earliest
topic增加分区
bin/kafka-topics.sh --alter --zookeeper(host:port) --topic demo-topic --partitions 12
指定topic创建消费者分组
bin/kafka-console-consumer.sh  --bootstrap-server=kafka-host(host:port) --topic demo-topic --consumer-property group.id=testPlatform
查看消费组组所属topic的消费情况
bin/kafka-consumer-groups.sh --bootstrap-server=kafka-host(host:port)  --group=demo-group --describ
显示所有消费者
bin/kafka-consumer-groups.sh --bootstrap-serverkafka-host(host:port) --list
获取正在消费的topic的group的offset
bin/kafka-consumer-groups.sh --describe --group demo-group --bootstrap-serverkafka-host(host:port)

##### 重设 consumer group的offset

确定topic作用域:
--all-topics   为consumer group下所有topic的所有分区调整位移
--topic t1 --topic t2  为指定的若干个topic的所有分区调整位移
--topic t1:0,1,2     为指定的topic分区调整位移


确定位移重设策略
--to-current                            把位移调整到分区当前位移.       
--to-datetime <String: datetime>       把位移调整到大于给定时间的最早位移处.datetime. Format: 'YYYY-MM-DDTHH:mm:SS.sss'  2020-07-01T12:00:00.000
--to-earliest                           把位移调整到分区当前最小位移
--to-latest                             把位移调整到分区当前最新位移        
--to-offset <Long: offset>              把位移调整到指定位移处
--shift-by N: 把位移调整到当前位移 + N处,注意N可以是负数,表示向前移动
--by-duration <duration>:把位移调整到距离当前时间指定间隔的位移处,duration格式是PnDTnHnMnS,比如PT0H5M0S
--from-file <file>:从CSV文件中读取调整策略
例:(--dry-run 不运行只查看结果,类似k8s里面;--execute执行)
按时间点重置(--to-datetime)
bin/kafka-consumer-groups.sh --bootstrap-server=localhost:9092 --topic=test --group=testPlatform  --execute --reset-offsets --to-datetime 2022-01-18T12:00:00.000

按offset重置 (--to-offset )
重置消费组下指定topic
bin/kafka-consumer-groups.sh --bootstrap-server=localhost:9092 --topic=test --group=testPlatform --execute --reset-offsets --to-offset 359905139  

重置消费组下面所以topic
bin/kafka-consumer-groups.sh --bootstrap-server=localhost:9092 --all-topics --group testPlatform --reset-offsets  --to-latest  --execute
指定offset与partition导出消息
bin/kafka-console-consumer.sh --bootstrap-server=kafka-host(host:port) --topic --topic=demo-topic  --offset 825000 --partition 0 >> messages.log

##### 修改topic的参数

kafka-configs.sh --zookeeper(host:port) --entity-type topics --entity-name demo-topic --alter --add-config max.message.bytes=1048576

##### 测试生产者性能脚本

bin/kafka-producer-perf-test.sh --topic demo-topic --num-records 10000000 --throughput -1 --record-size 1024 --producer-props bootstrap.servers=kafka-host(host:port) acks=-1 linger.ms=2000 compression.type=lz4

##### 测试消费者性能脚本

bin/kafka-consumer-perf-test.sh --broker-list kafka-host(host:port) --messages 10000000 --topic demo-topic

posted on 2023-03-03 18:47  cloud_wh  阅读(77)  评论(0编辑  收藏  举报

导航