专题:主题与分区

引言

        从KAFKA底层实现来说,主题和分区都是逻辑上的概念,分区可以有一至多个副本,每个副本对应一个日志文件夹,每个日志文件对应一至多个日志分段,每个日志分段还可以细分为索引文件、日志存储文件和快照文件等。

1.1. 主题管理

主题管理包括:创建、查看、删除和修改主题;

创建主题

使用--create --topic参数创建Topic

如果Broker端参数:auto.create.topics.enable 设置为true,则生产向一个尚未创建的主题发送消息时,会自动创建一个分区数为num.partitions(默认为1)、副本因子default.replication.factor(默认为1)的主题;其他会按照参数num.partitions和default.replication.factor的值来创建。

创建代码:

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic topic-create --partitions 3 --replication-factor 2

查看创建主题结果:

 1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topic-create
 2 Topic:topic-create    PartitionCount:3    ReplicationFactor:2    Configs:
 3     Topic: topic-create    Partition: 0    Leader: 2    Replicas: 2,1    Isr: 2,1
 4     Topic: topic-create    Partition: 1    Leader: 0    Replicas: 0,2    Isr: 0,2
 5     Topic: topic-create    Partition: 2    Leader: 1    Replicas: 1,0    Isr: 1,0

执行完脚本后,会在对应的日志目录下创建主题分区:

1 hadoop@ubuntu:/usr/local/kafka_2.11$ ls /tmp/kafka-logs
2 cleaner-offset-checkpoint  meta.properties  recovery-point-offset-checkpoint  replication-offset-checkpoint  topic-create-1  topic-create-2

同一个分区中的多个副本必须分布在不同的broker中,这才能提供有效地提供冗余数据,还能提供另外一种方式查看broker的分区副本的分配情况,zookeeper节点的/config/topic/[topic]下;

使用replica-assignment 参数创建topic

使用replica-assignment 参数,其创建时不需使用partition和replication-factor这二个参数;

如指定0:0,1:1这种会报AdminCommand-FailedException异常:

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic topic6 --replica-assignment 2:0,0:1,1:2,2:1
1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topic6
2 
3 Topic:topic6    PartitionCount:4    ReplicationFactor:2    Configs:
4     Topic: topic6    Partition: 0    Leader: 2    Replicas: 2,0    Isr: 2,0
5     Topic: topic6    Partition: 1    Leader: 0    Replicas: 0,1    Isr: 0,1
6     Topic: topic6    Partition: 2    Leader: 1    Replicas: 1,2    Isr: 1,2
7     Topic: topic6    Partition: 3    Leader: 2    Replicas: 2,1    Isr: 2,1

使用config参数创建topic

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic topic7 --partitions 1 --replication-factor 1 --config cleanup.policy=compact 
1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topic7
2 
3 Topic:topic7    PartitionCount:1    ReplicationFactor:1    Configs:cleanup.policy=compact
4     Topic: topic7    Partition: 0    Leader: 0    Replicas: 0    Isr: 0

查看主题 

使用参数查询topic

除了--describe参数之外,还可以通过三个参数查看附加功能:

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic-with-overrides

--topic-with-overrides : 找出所有包含覆盖配置的主题,只会列出与集群配置不一样的主题
--unavailable-partitions : 查看主题中没有leader副本的分区,这些分区已经处于离线状态,对于外界的生产者、消费者来说处于不可用的状态

--under-replicated-partitions : 找出包含失效副本的分区

有关副本有效性分析可参见:https://www.cnblogs.com/lenoblog/p/13984708.html

修改主题

当一个主题被创建之后,扔可以对其进行修改,如分区个数、配置等;

修改分区只能增加,不能减少

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic7 --partitions 3 
1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topic7
2 
3 Topic:topic7    PartitionCount:3    ReplicationFactor:1    Configs:cleanup.policy=compact
4     Topic: topic7    Partition: 0    Leader: 0    Replicas: 0    Isr: 0
5     Topic: topic7    Partition: 1    Leader: 1    Replicas: 1    Isr: 1
6     Topic: topic7    Partition: 2    Leader: 2    Replicas: 2    Isr: 2

变更参数配置

--max.message.types

--segment.types

--delete-config

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic7 --config max.message.types = 20000

2 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic7 --delete-config max.message.types  
3 --delete-config xxxx

配置管理

--entity-type : 指定操作配置的名称,只有4个值:topics、brokers、clients和users
--entity-name :查看配置的实体名称
1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-configs.sh -zookeeper localhost:2181 --describe --entity-type topics --entity-name topic7
2 
3 Configs for topic 'topic7' are cleanup.policy=compact

使用alter 指令变更配置时,需要配合add-config(增、改) 和delete-config (删)这两个参数一起使用;

主题端参数

参考地址:http://kafka.apache.org/documentation.html#topicconfigs

删除主题

删除主题需要先将 ,删除标识设置为true. 

1 hadoop@ubuntu:/usr/local/kafka_2.11$ vim config/server.properties

 delete.topic.enable=true
1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test

kafka-topics.sh删除只是在ZooKeeper中的/admin/delete_topics路径下创建一个与待删除主题同名的节点,以此标识该主题为待删除的状态;

真正的删除动作是由kafka控制器负责完成的。

ZooKeeper 中的元数据在/brokers/topics和/config/topics路径下;

1.2. KafkaAdminClient

使用KafkaadminClient类创建主题并执行相应的操作

1.3. 分区管理

1.3.1 优先副本

分区平衡

1.3.2 分区重分配

当要对集群中新增broker节点时,只有新创建的主题分区才有可能被分配到这个节点上,而之前的主题分区并不会自动分配到新加入的节点中。

所以需要让分区副本再次进行合理的分配,也就是所谓的分区重分配。

1.创建包含主题清单的JSON文件

{
        "topics":[
                {
                        "topic":"topic7"
                }
        ],
        "version":1
}

2.根据主题清单和broker节点清单生成一份重分配方案

hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --generate --topics-to-move-json-file reassign.json --broker-list 0,1

3.执行分配动作

根据第二步生成的JSON文件,执行

hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --execute --reassignment-json-file project.json

 验证查看分区重分配的进度

hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --verify --reassignment-json-file project.json

1.3.3 复制限流

数据复制会占用额外的资源,如果重分配的量太大,必须会严重影响整体的性能。

这时需要有一个限流的机制,可以对副本间的复制流量加以限制来保证重分配期间整体服务不会受太大的影响。

有两种实现方式:kafka-config.sh脚本和kafka-reassign-partitions.sh脚本

kafka-config.sh脚本主要以动态配置的方式来达到限流目的,在broker级别有两个与复制流相关的配置参数:follower.replication.throttled.rate和leader.replication.throttled.rate,

前者用于设置follower副本复制的速度,后面用于设置leader副本传输的速度,单位是B/s。

hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --entity-type brokers --entity-name1 -alter --add-configfollower.replication.throttled.rate=1024,leader.replication.throttled.rate=1024

1.3.4 修改副本因子

 创建主题之后我们还可以修改分区的个数,同样还可以修改副本因子(副本数)。

 

1.4. 选择合适的分区数

1.4.1 性能测试工具

使用kafka-producer-perf-test.sh和kafka-consumer-perf-test.sh

kafka-producer-perf-test.sh 对象

--topic : 主题名称
--num-records : 发送总条数
--record-size : 每条消息的字节数
--throughput : 进行限流控制,少于0时不限流
--producer-props : 生产者的配置文件
--print-metrics,打印指标信息
1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-producer-perf-test.sh --topic topic-1 --num-records 100000 --record-size 1024 --throughput 100 --producer-props bootstrap.servers=localhost:9094 acks=1
2 501 records sent, 99.4 records/sec (0.10 MB/sec), 4.8 ms avg latency, 152.0 max latency.
3 505 records sent, 101.0 records/sec (0.10 MB/sec), 47.3 ms avg latency, 419.0 max latency.
4 501 records sent, 100.2 records/sec (0.10 MB/sec), 2.7 ms avg latency, 45.0 max latency.
5 500 records sent, 99.9 records/sec (0.10 MB/sec), 2.2 ms avg latency, 36.0 max latency.
6 499 records sent, 99.6 records/sec (0.10 MB/sec), 2.5 ms avg latency, 45.0 max latency.
7 502 records sent, 100.4 records/sec (0.10 MB/sec), 6.3 ms avg latency, 88.0 max latency.

kafka-consumer-perf-test.sh 对象

1 hadoop@ubuntu:/usr/local/kafka_2.11$ bin/kafka-consumer-perf-test.sh --topic topic-1 --messages 1000000 --broker-list localhost:9094
2 start.time, end.time, data.consumed.in.MB, MB.sec, data.consumed.in.nMsg, nMsg.sec
3 2020-11-18 15:37:53:350, 2020-11-18 15:37:57:997, 111.2939, 23.9496, 113965, 24524.4244

 

1.4.2  吞吐量与分区数关系

 并不是的分区越大,吞吐量能力就越强。 

posted @ 2020-11-16 10:46  lenomail  阅读(180)  评论(0)    收藏  举报