您现在的位置是:首页 >技术杂谈 >kafka命令网站首页技术杂谈
kafka命令
查询kafka版本信息
kafka-configs.sh --describe --bootstrap-server localhost:9092 --version
查看所有topic
[root@m10 bin]# kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
kahn-topic-1
my_topic
x_topic-1
创建一个topic,名为x_topic-1
[root@m10 bin]# kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic x_topic-1
查看名为x_topic-1的主题
[root@m10 bin]# kafka-topics.sh --describe --zookeeper localhost:2181 --topic x_topic-1
Topic:x_topic-1 PartitionCount:5 ReplicationFactor:1 Configs:
Topic: x_topic-1 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: x_topic-1 Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: x_topic-1 Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: x_topic-1 Partition: 3 Leader: 0 Replicas: 0 Isr: 0
Topic: x_topic-1 Partition: 4 Leader: 0 Replicas: 0 Isr: 0
将名为x_topic-1的主题的分区增加到5(分区编号从0开始,分区只能增加,不能减少)
[root@m10 bin]# kafka-topics.sh --zookeeper localhost:2181 --alter --topic x_topic-1 --partitions 5
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
查询指定topic(x_topic-1)的offset最小值
[root@m10 bin]# kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 127.0.0.1:9092 -topic x_topic-1 --time -2
x_topic-1:0:0
x_topic-1:1:0
x_topic-1:2:0
x_topic-1:3:0
x_topic-1:4:0
查询指定topic(x_topic-1)的offset最大值
[root@m10 bin]# kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 127.0.0.1:9092 -topic x_topic-1 --time -1
x_topic-1:0:10
x_topic-1:1:0
x_topic-1:2:0
x_topic-1:3:0
x_topic-1:4:0
删除名为my_topic的topic
[root@m10 bin]# kafka-topics.sh --zookeeper localhost:2181 --topic my_topic --delete
Topic my_topic is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
对名为x_topic-1的topic设置过期时间为24小时=86400000ms
[root@m10 bin]# kafka-configs.sh --zookeeper localhost:2181 --alter --entity-name x_topic-1 --entity-type topics --add-config retention.ms=86400000
Completed Updating config for entity: topic 'x_topic-1'.
查看名为x_topic-1的topic的过期时间
[root@m10 bin]# kafka-configs.sh --zookeeper localhost:2181 --describe --entity-name x_topic-1 --entity-type topics
Configs for topic 'x_topic-1' are retention.ms=86400000
生产消息:向名为x_topic-1的topic主题生产消息
[root@m10 bin]# kafka-console-producer.sh --broker-list localhost:9092 --topic x_topic-1
>heihei1
>heihei2
>
>[root@m10 bin]#
消费消息:从名为x_topic-1的topic主题消费消息,--from-beginning从头开始
[root@m10 bin]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic x_topic-1 --from-beginning
quit
heihei1
"x_value-3690" #<-这几条消息只之前的,不知道为啥显示出来了
"x_value-3690-2" #<-这几条消息只之前的,不知道为啥显示出来了
"x_value-3690-3" #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 18} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 18} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 18} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 18} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 19} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 19} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 20} #<-这几条消息只之前的,不知道为啥显示出来了
{"name": "serena", "age": 21} #<-这几条消息只之前的,不知道为啥显示出来了
heihei2
消费消息:从名为x_topic-1的topic主题消费消息,从尾开始
[root@m10 bin]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic x_topic-1 --offset latest --partition 0
{"name": "serena", "age": 24}
{"name": "serena", "age": 25}
查看消费组列表
[root@m10 bin]# kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
console-consumer-35505 #<---如果还没指定过消费组,系统貌似会自动生成一个
查看指定消费group(上面查到的console-consumer-35505)状态和消费详情
[root@m10 bin]# kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group console-consumer-35505 --describe
Consumer group 'console-consumer-35505' has no active members
自带压测工具
[root@m10 bin]# kafka-producer-perf-test.sh --topic x_topic-1 --num-records 100 --record-size 1 --throughput 100 --producer-props bootstrap.servers=localhost:9092
100 records sent, 99.800399 records/sec (0.00 MB/sec), 5.05 ms avg latency, 120.00 ms max latency, 2 ms 50th, 13 ms 95th, 120 ms 99th, 120 ms 99.9th.