kafka 如何确定分区数及不同分区测试效果

 

Partitions设计目的kafka是基于文件存储,每个partition在存储层面试append log文件,任何发布到此partition的消息都会被直接追加到log文件的尾部。
通过分区,可以将日志内容分散到多个server上,来避免文件尺寸达到单机磁盘的上限,每个partition都会被当前server(kafka实例)保存,可以将一个topic切分到任意多个partitions。越多的partitions意味着可以容纳更多的consumer,有效提升并发消费的能力。
那么如何确定分区的数量呢?
分区数 = Tt / max(Tp, Tc)
Tp表示producer的吞吐量。测试producer通常是很容易的,因为它的逻辑非常简单,就是直接发送消息到Kafka就好了。Tc表示consumer的吞吐量。测试Tc通常与应用的关系更大, 因为Tc的值取决于你拿到消息之后执行什么操作,因此Tc的测试通常也要麻烦一些。总的目标吞吐量是Tt

创建的分区数不同,测试效果不一样

默认0个分区   分区数只能增加 不能减少  

[root@apache01 kafka]# bin/kafka-topics.sh --zookeeper apache02:2181,apache01:2181,apache03:2181 --describe --topic test
Topic:test    PartitionCount:1    ReplicationFactor:1    Configs:
    Topic: test    Partition: 0    Leader: 2    Replicas: 2    Isr: 2

[root@apache01 kafka]# bin/kafka-producer-perf-test.sh  --topic test --record-size 100 --num-records 100000 --throughput -1 --producer-props bootstrap.servers=apache01:9092,apache02:9092,apache03:9092
100000 records sent, 23764.258555 records/sec (2.27 MB/sec), 1647.63 ms avg latency, 2280.00 ms max latency, 1606 ms 50th, 2248 ms 95th, 2262 ms 99th, 2280 ms 99.9th.
 

修改分区数量为3
[root@apache01 kafka]#  bin/kafka-topics.sh --zookeeper apache02:2181  --alter --topic test --partitions 3
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@apache01 kafka]# bin/kafka-topics.sh --zookeeper apache02:2181,apache01:2181,apache03:2181 --describe --topic test
Topic:test    PartitionCount:3    ReplicationFactor:1    Configs:
    Topic: test    Partition: 0    Leader: 2    Replicas: 2    Isr: 2
    Topic: test    Partition: 1    Leader: 0    Replicas: 0    Isr: 0
    Topic: test    Partition: 2    Leader: 1    Replicas: 1    Isr: 1
[root@apache01 kafka]# bin/kafka-producer-perf-test.sh  --topic test --record-size 100 --num-records 100000 --throughput -1 --producer-props bootstrap.servers=apache01:9092,apache02:9092,apache03:9092
100000 records sent, 109289.617486 records/sec (10.42 MB/sec), 201.79 ms avg latency, 313.00 ms max latency, 237 ms 50th, 293 ms 95th, 306 ms 99th, 312 ms 99.9th.

修改分区数量为6
[root@apache01 kafka]#  bin/kafka-topics.sh --zookeeper apache02:2181  --alter --topic test --partitions 6
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@apache01 kafka]# bin/kafka-topics.sh --zookeeper apache02:2181,apache01:2181,apache03:2181 --describe --topic test
Topic:test    PartitionCount:6    ReplicationFactor:1    Configs:
    Topic: test    Partition: 0    Leader: 2    Replicas: 2    Isr: 2
    Topic: test    Partition: 1    Leader: 0    Replicas: 0    Isr: 0
    Topic: test    Partition: 2    Leader: 1    Replicas: 1    Isr: 1
    Topic: test    Partition: 3    Leader: 2    Replicas: 2    Isr: 2
    Topic: test    Partition: 4    Leader: 0    Replicas: 0    Isr: 0
    Topic: test    Partition: 5    Leader: 1    Replicas: 1    Isr: 1
[root@apache01 kafka]# 
[root@apache01 kafka]# 
[root@apache01 kafka]# bin/kafka-producer-perf-test.sh  --topic test --record-size 100 --num-records 100000 --throughput -1 --producer-props bootstrap.servers=apache01:9092,apache02:9092,apache03:9092
100000 records sent, 158982.511924 records/sec (15.16 MB/sec), 50.68 ms avg latency, 160.00 ms max latency, 50 ms 50th, 90 ms 95th, 101 ms 99th, 121 ms 99.9th.

修改分区数量为9
[root@apache01 kafka]#  bin/kafka-topics.sh --zookeeper apache02:2181  --alter --topic test --partitions 9
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@apache01 kafka]# bin/kafka-producer-perf-test.sh  --topic test --record-size 100 --num-records 100000 --throughput -1 --producer-props bootstrap.servers=apache01:9092,apache02:9092,apache03:9092
100000 records sent, 167785.234899 records/sec (16.00 MB/sec), 47.10 ms avg latency, 141.00 ms max latency, 42 ms 50th, 98 ms 95th, 110 ms 99th, 129 ms 99.9th.

修改分区数量为15
[root@apache01 kafka]#  bin/kafka-topics.sh --zookeeper apache02:2181  --alter --topic test --partitions 15
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@apache01 kafka]# bin/kafka-producer-perf-test.sh  --topic test --record-size 100 --num-records 100000 --throughput -1 --producer-props bootstrap.servers=apache01:9092,apache02:9092,apache03:9092
100000 records sent, 159489.633174 records/sec (15.21 MB/sec), 74.48 ms avg latency, 240.00 ms max latency, 42 ms 50th, 207 ms 95th, 222 ms 99th, 237 ms 99.9th.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
增加 Kafka 分区数需要进行如下的步骤: 1. 修改 `server.properties` 文件。 在 Kafka 集群中,每个 Kafka Broker 都会有一个 `server.properties` 文件。你需要找到这个文件并修改以下两个参数: ``` num.partitions=NEW_PARTITION_COUNT auto.create.topics.enable=true ``` 将 `NEW_PARTITION_COUNT` 修改为你期望的新分区数,然后将 `auto.create.topics.enable` 参数设置为 `true`,以便创建新的分区。 2. 创建一个新的主题,或者在现有主题上增加分区。 你可以使用 Kafka 的命令行工具 `kafka-topics` 来创建一个新的主题,或者在现有主题上增加分区。例如,如果你想要在主题 `my-topic` 上增加 2 个分区,你可以运行以下命令: ``` kafka-topics --zookeeper <your_zookeeper_address> --alter --topic my-topic --partitions 5 ``` 注意,这个命令会立即生效,并且可能会影响你的生产环境。因此,在进行这个操作之前,确保对你的 Kafka 集群进行了充分的测试。 3. 重新分配分区。 当你增加了分区之后,Kafka 集群会自动将现有的分区重新分配到不同的 Broker 上。你可以使用 Kafka 的命令行工具 `kafka-reassign-partitions` 来重新分配分区。这个命令会产生一个 JSON 文件,其中包含了新的分区分配方案。你需要将这个文件提交给 Kafka 集群,以便重新分配分区。例如,你可以运行以下命令: ``` kafka-reassign-partitions --zookeeper <your_zookeeper_address> --reassignment-json-file <path_to_json_file> --execute ``` 这个命令会将新的分区分配方案提交到 Kafka 集群,并且开始重新分配分区。这个过程可能需要一些时间,具体取决于你的 Kafka 集群的规模和负载情况。在这个过程中,你的 Kafka 集群可能会出现一些不稳定的情况,因此请确保在进行这个操作之前备份你的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值