kafka 查看分区数_kafka_2.11-2.0.0_常用操作

以下操作可以在mini01、mini02、mini03任意一台操作即可

1. kafka通过网页管理

参考博文:kafka集群管理工具kafka-manager部署安装

2d8e77c4189cb7791d389d4e1c9e2279.png

2. 创建topic

1 # 参数说明 --replication-factor 2 表示有2个副本2 # --partitions 4 表示有4个分区3 [yun@mini01 ~]$ kafka-topics.sh --create --zookeeper mini01:2181 --replication-factor 2 --partitions 4 --topic test4 Created topic "test".5 [yun@mini01 ~]$ kafka-topics.sh --create --zookeeper mini01:2181 --replication-factor 3 --partitions 4 --topic zhang6 Created topic "zhang".7 [yun@mini01 ~]$ kafka-topics.sh --list --zookeeper mini01:2181  # 再次查看8 zhang9 test

2.1. 各主机信息查看

mini01

1 [yun@mini01 logs]$ pwd2 /app/kafka/logs3 [yun@mini01 logs]$ ll 4 total 1605 ………………6 drwxrwxr-x 2 yun yun   141 Sep 15 18:53 test-17 drwxrwxr-x 2 yun yun   141 Sep 15 18:53 test-28 drwxrwxr-x 2 yun yun   141 Sep 15 18:53 test-3

mini02

1 [yun@mini02 logs]$ pwd2 /app/kafka/logs3 [yun@mini02 logs]$ ll4 total 2605 ………………6 drwxrwxr-x 2 yun yun    141 Sep 15 18:53 test-07 drwxrwxr-x 2 yun yun    141 Sep 15 18:53 test-2

mini03

1 [yun@mini03 logs]$ pwd2 /app/kafka/logs3 [yun@mini03 logs]$ ll4 total 1325 ………………6 drwxrwxr-x 2 yun yun   141 Sep 15 18:53 test-07 drwxrwxr-x 2 yun yun   141 Sep 15 18:53 test-18 drwxrwxr-x 2 yun yun   141 Sep 15 18:53 test-3

3. 修改topic

3.1. 增加分区数

注意:分区数不能减少

Kafka目前不支持减少主题的分区数量。

 1 [yun@mini01 ~]$ kafka-topics.sh --list --zookeeper mini01:2181  2 __consumer_offsets 3 test 4 test01 5 test02 6 test03 7 test04 8 zhang 9 [yun@mini01 ~]$ kafka-topics.sh --describe --zookeeper mini01:2181 --topic test0110 Topic:test01    PartitionCount:5    ReplicationFactor:1    Configs:11     Topic: test01    Partition: 0    Leader: 0    Replicas: 0    Isr: 012     Topic: test01    Partition: 1    Leader: 1    Replicas: 1    Isr: 113     Topic: test01    Partition: 2    Leader: 2    Replicas: 2    Isr: 214     Topic: test01    Partition: 3    Leader: 0    Replicas: 0    Isr: 015     Topic: test01    Partition: 4    Leader: 1    Replicas: 1    Isr: 116 [yun@mini01 ~]$ kafka-topics.sh --alter --zookeeper mini01:2181 --partitions 2 --topic test01 # 失败,分区数不能减少 17 WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected18 Error while executing topic command : The number of partitions for a topic can only be increased. Topic test01 currently has 5 partitions, 2 would not be an increase.19 [2018-09-16 09:12:40,034] ERROR org.apache.kafka.common.errors.InvalidPartitionsException: The number of partitions for a topic can only be increased. Topic test01 currently has 5 partitions, 2 would not be an increase.20  (kafka.admin.TopicCommand$)21 [yun@mini01 ~]$ kafka-topics.sh --alter --zookeeper mini01:2181 --partitions 7 --topic test01  # 增加分区数 22 WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected23 Adding partitions succeeded!24 [yun@mini01 ~]$ kafka-topics.sh --describe --zookeeper mini01:2181 --topic test0125 Topic:test01    PartitionCount:7    ReplicationFactor:1    Configs:26     Topic: test01    Partition: 0    Leader: 0    Replicas: 0    Isr: 027     Topic: test01    Partition: 1    Leader: 1    Replicas: 1    Isr: 128     Topic: test01    Partition: 2    Leader: 2    Replicas: 2    Isr: 229     Topic: test01    Partition: 3    Leader: 0    Replicas: 0    Isr: 030     Topic: test01    Partition: 4    Leader: 1    Replicas: 1    Isr: 131     Topic: test01    Partition: 5    Leader: 2    Replicas: 2    Isr: 232     Topic: test01    Partition: 6    Leader: 0    Replicas: 0    Isr: 0

4. 删除topic

1 # server.properties中设置delete.topic.enable=true 【当前版本默认就是true】否则只是标记删除或者直接重启2 [yun@mini01 ~]$ kafka-topics.sh --delete --zookeeper mini01:2181 --topic test3 Topic test is marked for deletion.4 Note: This will have no impact if delete.topic.enable is not set to true.5 [yun@mini01 ~]$ kafka-topics.sh --list --zookeeper mini01:2181  # 再次查看   只有 zhang,则表示真的删除了6 zhang

5. 查看所有topic

1 [yun@mini01 ~]$ kafka-topics.sh --list --zookeeper mini01:2181 2 __consumer_offsets3 test4 zhang

6. 查看某个Topic的详情

1 [yun@mini01 ~]$ kafka-topics.sh --describe --zookeeper mini01:2181 --topic zhang 2 Topic:zhang    PartitionCount:4    ReplicationFactor:3    Configs:3     Topic: zhang    Partition: 0    Leader: 1    Replicas: 1,2,0    Isr: 1,2,04     Topic: zhang    Partition: 1    Leader: 2    Replicas: 2,0,1    Isr: 2,0,15     Topic: zhang    Partition: 2    Leader: 0    Replicas: 0,1,2    Isr: 0,1,26     Topic: zhang    Partition: 3    Leader: 1    Replicas: 1,0,2    Isr: 1,0,2

7. 通过shell命令生产消息

7.1. 输入单条数据

 1 [yun@mini01 ~]$ kafka-console-producer.sh --broker-list mini01:9092 --topic zhang 2 >111 3 >222 4 >333 5 >444 6 >555 7 >666 8 >777 9 >88810 >999

7.2. 批量导入数据

1 [yun@mini01 zhangliang]$ kafka-console-producer.sh --broker-list mini01:9092 --topic liang < 001.info

8. 通过shell命令消费消息

 1 # --from-beginning 从最开始读取 2 # kafka-console-consumer.sh --zookeeper mini01:2181 --from-beginning --topic zhang  # 老版本 3 [yun@mini01 ~]$ kafka-console-consumer.sh --bootstrap-server mini01:9092 --from-beginning --topic zhang 4 111 5 555 6 999 7 333 8 777 9 44410 88811 22212 666

9. 消费组消费

9.1. 创建topic

 1 [yun@mini01 ~]$ kafka-topics.sh --create --zookeeper mini01:2181 --replication-factor 1 --partitions 4 --topic order 2 Created topic "order". 3 [yun@mini01 ~]$ kafka-topics.sh --list --zookeeper mini01:2181  # 查看所有topic列表 4 __consumer_offsets 5 order 6 test 7 zhang 8 [yun@mini01 ~]$ kafka-topics.sh --describe --zookeeper mini01:2181 --topic order  # 查看topic详情 9 Topic:order    PartitionCount:4    ReplicationFactor:1    Configs:10     Topic: order    Partition: 0    Leader: 0    Replicas: 0    Isr: 011     Topic: order    Partition: 1    Leader: 1    Replicas: 1    Isr: 112     Topic: order    Partition: 2    Leader: 2    Replicas: 2    Isr: 213     Topic: order    Partition: 3    Leader: 0    Replicas: 0    Isr: 0

9.2. 生产消息

1 [yun@mini01 ~]$ kafka-console-producer.sh --broker-list mini01:9092 --topic order2 >1113 >2224 >3335 >4446 >555

9.3. 消费组消费消息

mini02机器上运行

1 # --group  指定组2 [yun@mini02 ~]$ kafka-console-consumer.sh --bootstrap-server mini01:9092 --topic order --group order-group

mini03机器上运行

1 # --group  指定组2 [yun@mini03 ~]$ kafka-console-consumer.sh --bootstrap-server mini01:9092 --topic order --group order-group3 4 # 新开一个窗口执行5 [yun@mini03 ~]$ kafka-console-consumer.sh --bootstrap-server mini01:9092 --topic order --group order-group

表示order-group消费组有3个消费者,消费topic order的信息。

9.4. 消费组消费位置信息查看

 1 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group order-group  # 查看消费情况 2  3 TOPIC    PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG   CONSUMER-ID                                     HOST            CLIENT-ID 4 order    0          4               4               0     consumer-1-2e9805db-e021-4595-8c62-92f8691fbf20 /172.16.1.13    consumer-1 5 order    1          5               5               0     consumer-1-2e9805db-e021-4595-8c62-92f8691fbf20 /172.16.1.13    consumer-1 6 order    2          5               5               0     consumer-1-9e65dcfb-246f-4043-aaf7-3ee83532237f /172.16.1.13    consumer-1 7 order    3          4               4               0     consumer-1-ee17939d-1ffe-42c7-8261-b19be8acea43 /172.16.1.12    consumer-1 8 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group order-group --members --verbose 9 10 CONSUMER-ID                                     HOST            CLIENT-ID       #PARTITIONS     ASSIGNMENT11 consumer-1-9e65dcfb-246f-4043-aaf7-3ee83532237f /172.16.1.13    consumer-1      1               order(2)12 consumer-1-2e9805db-e021-4595-8c62-92f8691fbf20 /172.16.1.13    consumer-1      2               order(0,1)13 consumer-1-ee17939d-1ffe-42c7-8261-b19be8acea43 /172.16.1.12    consumer-1      1               order(3)

10. 管理消费组

10.1. 查看所有消费组

1 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --list2 console-consumer-267273 console-consumer-929844 console-consumer-607555 console-consumer-116616 console-consumer-317137 console-consumer-202448 console-consumer-65733

10.2. 查看消费组消费情况【消费位置】

 1 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group console-consumer-26727   2 Consumer group 'console-consumer-26727' has no active members. 3  4 TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID 5 zhang           3          11              11              0               -               -               - 6 zhang           0          9               9               0               -               -               - 7 zhang           2          8               8               0               -               -               - 8 zhang           1          11              11              0               -               -               - 9 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group console-consumer-65733  10 11 TOPIC  PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG  CONSUMER-ID                                     HOST            CLIENT-ID12 zhang  0          11              11              0    consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13    consumer-113 zhang  1          12              12              0    consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13    consumer-114 zhang  2          10              10              0    consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13    consumer-115 zhang  3          12              12              0    consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13    consumer-1

--members

1 # --members 此选项提供使用者组中所有活动成员的列表。2 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group console-consumer-65733 --members  3 4 CONSUMER-ID                                     HOST            CLIENT-ID       #PARTITIONS     5 consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13    consumer-1      4      

--verbose

1 # --verbose 这个选项还提供了分配给每个成员的分区。2 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group console-consumer-65733 --members --verbose3 4 CONSUMER-ID                                     HOST            CLIENT-ID       #PARTITIONS     ASSIGNMENT5 consumer-1-17c812f0-116b-42a9-88d8-90d1a85949e1 /172.16.1.13    consumer-1      4               zhang(0,1,2,3)

--state

1 # --state  这个选项提供了有用的组级信息。2 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --describe --group console-consumer-65733 --state3 4 COORDINATOR (ID)          ASSIGNMENT-STRATEGY       STATE                #MEMBERS5 mini01:9092 (0)           range                     Stable               1

10.3. 删除一个或多个用户组

 1 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --list 2 console-consumer-3826 3 console-consumer-92984 4 console-consumer-60755 5 console-consumer-11661 6 console-consumer-31713 7 console-consumer-20244 8 console-consumer-65733 9 # 删除一个或多个组10 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --delete --group console-consumer-11661 --group console-consumer-3171311 Deletion of requested consumer groups ('console-consumer-31713', 'console-consumer-11661') was successful.12 [yun@mini01 ~]$ kafka-consumer-groups.sh --bootstrap-server mini01:9092 --list13 console-consumer-382614 console-consumer-9298415 console-consumer-6075516 console-consumer-2024417 console-consumer-65733

11. 增加副本因子

1 [yun@mini01 kafka_20180916]$ kafka-topics.sh --create --zookeeper mini01:2181 --replication-factor 1 --partitions 4 --topic order2 Created topic "order".3 [yun@mini01 kafka_20180916]$ kafka-topics.sh --describe --zookeeper mini01:2181 --topic order4 Topic:order    PartitionCount:4    ReplicationFactor:1    Configs:5     Topic: order    Partition: 0    Leader: 0    Replicas: 0    Isr: 06     Topic: order    Partition: 1    Leader: 1    Replicas: 1    Isr: 17     Topic: order    Partition: 2    Leader: 2    Replicas: 2    Isr: 28     Topic: order    Partition: 3    Leader: 0    Replicas: 0    Isr: 0

要求:topic order 的副本数由1变为2, 之前每个分区在哪台机器上在上面已给出。

说明:part 0分布在群集0,1; part 1分布在集群1,2;part 2 分布在集群2,0;part 3分布在集群0,1。

11.1. 创建一个重新调整计划文件

 1 [yun@mini01 kafka_20180916]$ cat increase-replication-factor.json 2 { 3   "version":1, 4   "partitions":[ 5     {"topic": "order","partition": 0,"replicas": [0,1]}, 6     {"topic": "order","partition": 1,"replicas": [1,2]}, 7     {"topic": "order","partition": 2,"replicas": [2,0]}, 8     {"topic": "order","partition": 3,"replicas": [0,1]} 9   ]10 }

11.2. 语句执行

1 [yun@mini01 kafka_20180916]$ kafka-reassign-partitions.sh --zookeeper mini01:2181 --reassignment-json-file increase-replication-factor.json --execute 2 Current partition replica assignment3 4 {"version":1,"partitions":[{"topic":"order","partition":2,"replicas":[2],"log_dirs":["any"]},{"topic":"order","partition":1,"replicas":[1],"log_dirs":["any"]},{"topic":"order","partition":3,"replicas":[0],"log_dirs":["any"]},{"topic":"order","partition":0,"replicas":[0],"log_dirs":["any"]}]}5 6 Save this to use as the --reassignment-json-file option during rollback7 Successfully started reassignment of partitions.

11.3. 查看是否执行成功

1 [yun@mini01 kafka_20180916]$ kafka-reassign-partitions.sh --zookeeper mini01:2181 --reassignment-json-file increase-replication-factor.json --verify2 Status of partition reassignment: 3 Reassignment of partition order-0 completed successfully4 Reassignment of partition order-1 completed successfully5 Reassignment of partition order-2 completed successfully6 Reassignment of partition order-3 completed successfully

11.4. 再次查看该topic详情

1 [yun@mini01 kafka_20180916]$ kafka-topics.sh --describe --zookeeper mini01:2181 --topic order  # 由下可见分配成功2 Topic:order    PartitionCount:4    ReplicationFactor:2    Configs:3     Topic: order    Partition: 0    Leader: 0    Replicas: 0,1    Isr: 0,14     Topic: order    Partition: 1    Leader: 1    Replicas: 1,2    Isr: 1,25     Topic: order    Partition: 2    Leader: 2    Replicas: 2,0    Isr: 2,06     Topic: order    Partition: 3    Leader: 0    Replicas: 0,1    Isr: 0,1

12. 创建partitions时在broker的分配机制

1 kafka-topics.sh --create --zookeeper mini01:2181 --replication-factor 1 --partitions 5 --topic test012 kafka-topics.sh --create --zookeeper mini01:2181 --replication-factor 1 --partitions 11 --topic test02

注意在各机器上partitions的分布

 1 mini01 2     test01-0 3     test01-3 4     test02-2 5     test02-5 6     test02-8 7  8 mini02 9     test01-110     test01-411     test02-012     test02-313     test02-614     test02-915 16 mini0317     test01-218     test02-119     test02-1020     test02-421     test02-7

版权声明:本文为weixin_30583711原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。