kafka 常用命令
创建 topic
# 参数说明 --replication-factor 2 表示有2个副本2 # --partitions 4 表示有4个分区3
[root@bogon bin]# ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 4 --topic test4 Created topic "test"
Created topic "test4".
[root@bogon bin]# ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 4 --topic zhang6 Created topic "zhang"
Created topic "zhang6".
[root@bogon bin]# ./kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
test4
zhang6
查看某个 Topic 的详情
注意:分区数不能减少
Kafka 目前不支持减少主题的分区数量。
[root@bogon bin]# ./kafka-topics.sh --describe --zookeeper localhost:2181 --topic test4
Topic:test4 PartitionCount:4 ReplicationFactor:1 Configs:
Topic: test4 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: test4 Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: test4 Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: test4 Partition: 3 Leader: 0 Replicas: 0 Isr: 0
删除 topic
# server.properties中设置delete.topic.enable=true 【当前版本默认就是true】否则只是标记删除或者直接重启2
[root@bogon bin]# ./kafka-topics.sh --delete --zookeeper localhost:2181 --topic test4
Topic test4 is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
查看所有 topic
[root@bogon bin]# ./kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
zhang6
通过 shell 命令生产消息
输入单条数据
[root@bogon bin]# ./kafka-console-producer.sh --broker-list 192.168.56.111:9092 --topic zhang6
>111
>222
批量导入数据
[root@bogon bin]# ./kafka-console-producer.sh --broker-list 192.168.56.111:9092 --topic zhang6 < 001.info
>>>>>>>>>>>>>>>>>>
通过 shell 命令消费消息
# --from-beginning 从最开始读取 2
[root@bogon bin]# ./kafka-console-consumer.sh --zookeeper 192.168.56.111:2181 --from-beginning --topic zhang6 # 老版本
[root@bogon bin]# ./kafka-console-consumer.sh --bootstrap-server 192.168.56.111:2181 --from-beginning --topic zhang6
4 111 5 555 6 999 7 333 8 777 9 44410 88811 22212 666
消费组消费消息
# --group 指定组2
[root@bogon bin]# ./kafka-console-consumer.sh --bootstrap-server 192.168.56.111:9092 --topic zhang6 --group order-group
消费组消费位置信息查看
[root@bogon bin]# ./kafka-consumer-groups.sh --bootstrap-server 192.168.56.111:9092 --describe --group order-group
Note: This will not show information about old Zookeeper-based consumers.
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
zhang6 0 11 11 0 consumer-1-93e889ae-0b19-470b-8a92-06909e0a46c5 /192.168.56.111 consumer-1
zhang6 1 15 15 0 consumer-1-93e889ae-0b19-470b-8a92-06909e0a46c5 /192.168.56.111 consumer-1
zhang6 2 12 12 0 consumer-1-93e889ae-0b19-470b-8a92-06909e0a46c5 /192.168.56.111 consumer-1
zhang6 3 13 13 0 consumer-1-93e889ae-0b19-470b-8a92-06909e0a46c5 /192.168.56.111 consumer-1
管理消费组
查看所有消费组
[root@bogon bin]# ./kafka-consumer-groups.sh --bootstrap-server 192.168.56.111:9092 --list
Note: This will not show information about old Zookeeper-based consumers.
order-group
# --members 此选项提供使用者组中所有活动成员的列表。
[root@bogon bin]# ./kafka-consumer-groups.sh --bootstrap-server 192.168.56.111:9092 --describe --group order-group --members
Note: This will not show information about old Zookeeper-based consumers.
CONSUMER-ID HOST CLIENT-ID #PARTITIONS
consumer-1-93e889ae-0b19-470b-8a92-06909e0a46c5 /192.168.56.111 consumer-1 4 HOST CLIENT-ID #PARTITIONS 5 consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13 consumer-1 4
# --verbose 这个选项还提供了分配给每个成员的分区。
[root@bogon bin]# ./kafka-consumer-groups.sh --bootstrap-server 192.168.56.111:9092 --describe --group order-group --members --verbose
Note: This will not show information about old Zookeeper-based consumers.
CONSUMER-ID HOST CLIENT-ID #PARTITIONS ASSIGNMENT
consumer-1-93e889ae-0b19-470b-8a92-06909e0a46c5 /192.168.56.111 consumer-1 4 zhang6(0,1,2,3)
# --state 这个选项提供了有用的组级信息。
[root@bogon bin]# ./kafka-consumer-groups.sh --bootstrap-server 192.168.56.111:9092 --describe --group order-group --state
Note: This will not show information about old Zookeeper-based consumers.
COORDINATOR (ID) ASSIGNMENT-STRATEGY STATE #MEMBERS
192.168.56.111:9092 (0) range Stable 1