按照分区进行存储数据 kafka的分区和hadoop分区类似
8.创建主题:创建名为“my-topichyxy”的主题,并设置其分区为2,复本为2
$>kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 2 --topic my-topichyxy
Created topic "my-topichyxy".
$> kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-topichyxy
pic:my-topichyxy PartitionCount:2 ReplicationFactor:2 Configs:
Topic: my-topichyxy Partition: 0 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: my-topichyxy Partition: 1 Leader: 0 Replicas: 0,1 Isr: 0,1
查看master和slave1[log.dirs]目录下,主题生成的结果 其分区为2,复本为2
验证Partition0 副本数Replicas(2,0):
broker.id=0 ---> master-00
[hyxy@master master-00]$ ls
my-topichyxy-1 my-topichyxy-0
broker.id=1 ---> master-11
[hyxy@master master-11]$ ls
my-topichyxy-1
broker.id=2 ---> slave1-00
[hyxy@slave1 slave1-00]$ ls
my-topichyxy-0
broker.id=3 ---> slave1-11
[hyxy@slave1 slave1-11]$ ls
9.启动Producer
$>kafka-console-producer.sh --topic my-topichyxy --broker-list master:9092,master:9093,slave1:9092,slave1:9093
10.启动消费者
master$>kafka-console-consumer.sh --zookeeper localhost:2181 --topic my-topichyxy --from-beginning
slave1$>kafka-console-consumer.sh --zookeeper localhost:2181 --topic my-topichyxy --from-beginning
验证: kafka的分区和hadoop分区类似:
producer发送一条消息 1 %2 =1 所以放在第一个分区中 my-topichyxy-0 ;查看my-topichyxy-1 并没有数据
producer再次发送一条消息 继续给 2 3 4 5 查看分区数