kafka分區和副本如何分配


 
申請一個topic,官方命令如下:
bin/kafka-topics.sh 
--create //操作類型
--zookeeper localhost:2181 //kafka依賴的zookeeper地址
--replication-factor  2//副本因子
--partitions 1 //分區數量
--topic test //topic 名稱
 
分區和副本的分配方式如下圖:
可能通過上圖,你雲里霧里,既然知道命令,我們就通過命令來看看,到底如何分配。
bin/kafka-topics.sh --create --zookeeper localhost:2181 
--replication-factor 2 
--partitions 5 
--topic test-part
 
通過命令可以看出 topic:test-part 指定了 5個分區,拷貝因子為2(代表只有一個副本),通過 執行命令 查看 topic:test-part 信息如下:
 
[root@VM_15_71_centos kafka_2.11-1.0.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test-part
[2019-06-28 01:23:50,646] INFO Accepted socket connection from /127.0.0.1:41204 (org.apache.zookeeper.server.NIOServerCnxnFactory)
[2019-06-28 01:23:50,646] INFO Client attempting to establish new session at /127.0.0.1:41204 (org.apache.zookeeper.server.ZooKeeperServer)
[2019-06-28 01:23:50,649] INFO Established session 0x16b99ec1ce1000c with negotiated timeout 30000 for client /127.0.0.1:41204 (org.apache.zookeeper.server.ZooKeeperServer)
Topic:test-part	PartitionCount:5	ReplicationFactor:2	Configs:
    	Topic: test-part	Partition: 0	Leader: 0	Replicas: 0,2	Isr: 0,2
    	Topic: test-part	Partition: 1	Leader: 1	Replicas: 1,3	Isr: 1,3
    	Topic: test-part	Partition: 2	Leader: 2	Replicas: 2,0	Isr: 2,0
    	Topic: test-part	Partition: 3	Leader: 3	Replicas: 3,1	Isr: 3,1
	Topic: test-part	Partition: 4	Leader: 0	Replicas: 0,3	Isr: 0,3
[2019-06-28 01:23:50,899] INFO Processed session termination for sessionid: 0x16b99ec1ce1000c (org.apache.zookeeper.server.PrepRequestProcessor)
[2019-06-28 01:23:50,904] INFO Closed socket connection for client /127.0.0.1:41204 which had sessionid 0x16b99ec1ce1000c (org.apache.zookeeper.server.NIOServerCnxn)

 

通過上邊的描述信息,可以得到如下圖分配方式 -L:Leader  -C:Replicas(副本)

然后,我又執行了如下命令:
bin/kafka-topics.sh --create --zookeeper localhost:2181 
--replication-factor 2 
--partitions 6 
--topic test-part-1

得到分配圖如下:

通過以上兩次創建topic對比,
 
第一次(5分區,2復制因子) test-part
第二次(6分區,2復制因子)  test-part-1
分區0所在broker
0
3
副本與leader所在的關系
leader所在broker.id + 2,即副本所在broker.id
leader所在broker.id + 2,即副本所在broker.id
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM