利用Kafka动态调整topic分区partition

 更新时间:2022年12月27日 11:38:38   作者:russle  
这篇文章主要介绍了利用Kafka动态调整topic分区partition问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Kafka动态调整topic分区partition

在使用kafka时,初期创建topic时所指定的topic属性有时会需要修改,如何动态修改kafka topic属性?kafka提供了命令行工具—kafka-topics.sh.

kafka-topics.sh工具介绍

kafka-topics.sh工具也是我们用来创建topic、查看topic详情的工具。

直接运行kafka-topics.sh可以看出,它是用来创建、删除、查看以及更新topic的

root@ubuntu:/opt/kafka_2.11-1.1.0/bin# ./kafka-topics.sh
Create, delete, describe, or change a topic.
Option Description

–alter Alter the number of partitions,
replica assignment, and/or
configuration for the topic.
–config <String: name=value> A topic configuration override for
…

更新或者修改topic

注意:我的kafka版本是1.1.0, 并且我只有一个broker。

1, 首先我们创建一个topic,然后查看详情

root@ubuntu:/opt/kafka_2.11-1.1.0/bin# ./kafka-topics.sh --create --zookeeper 192.168.119.131:2181 --replication-factor 1 --partitions 4 --topic yqtopic1
Created topic “yqtopic1”.
root@ubuntu:/opt/kafka_2.11-1.1.0/bin#

root@ubuntu:/opt/kafka_2.11-1.1.0/bin# ./kafka-topics.sh --describe --zookeeper 192.168.119.131:2181 --topic yqtopic1
Topic:yqtopic1 PartitionCount:4 ReplicationFactor:1 Configs:
Topic: yqtopic1 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 3 Leader: 0 Replicas: 0 Isr: 0
root@ubuntu:/opt/kafka_2.11-1.1.0/bin#

2,修改刚创建的topic,并查看修改的情况

将分区数有4修改为12

root@ubuntu:/opt/kafka_2.11-1.1.0/bin# ./kafka-topics.sh --alter --zookeeper 192.168.119.131:2181 --topic yqtopic1 --partitions 12
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@ubuntu:/opt/kafka_2.11-1.1.0/bin#

root@ubuntu:/opt/kafka_2.11-1.1.0/bin# ./kafka-topics.sh --describe --zookeeper 192.168.119.131:2181 --topic yqtopic1 Topic:yqtopic1 PartitionCount:12 ReplicationFactor:1 Configs:
Topic: yqtopic1 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 3 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 4 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 5 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 6 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 7 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 8 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 9 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 10 Leader: 0 Replicas: 0 Isr: 0
Topic: yqtopic1 Partition: 11 Leader: 0 Replicas: 0 Isr: 0
root@ubuntu:/opt/kafka_2.11-1.1.0/bin# ls -al /tmp/kafka-logs/
total 72
drwxr-xr-x 14 root root 4096 Oct 13 14:34 .
drwxrwxrwt 17 root root 4096 Oct 13 14:34 …
-rw-r–r-- 1 root root 0 Oct 13 14:10 cleaner-offset-checkpoint
-rw-r–r-- 1 root root 0 Oct 13 14:10 .lock
-rw-r–r-- 1 root root 4 Oct 13 14:33 log-start-offset-checkpoint
-rw-r–r-- 1 root root 54 Oct 13 14:10 meta.properties
-rw-r–r-- 1 root root 163 Oct 13 14:33 recovery-point-offset-checkpoint
-rw-r–r-- 1 root root 163 Oct 13 14:34 replication-offset-checkpoint
drwxr-xr-x 2 root root 4096 Oct 13 14:20 yqtopic1-0
drwxr-xr-x 2 root root 4096 Oct 13 14:20 yqtopic1-1
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-10
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-11
drwxr-xr-x 2 root root 4096 Oct 13 14:20 yqtopic1-2
drwxr-xr-x 2 root root 4096 Oct 13 14:20 yqtopic1-3
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-4
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-5
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-6
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-7
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-8
drwxr-xr-x 2 root root 4096 Oct 13 14:33 yqtopic1-9
root@ubuntu:/opt/kafka_2.11-1.1.0/bin#

修改后的截图如下

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java随机生成时间字符串的方法

    java随机生成时间字符串的方法

    这篇文章主要为大家详细介绍了java随机生成时间字符串的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Springboot整合MybatisPlus的实现过程解析

    Springboot整合MybatisPlus的实现过程解析

    这篇文章主要介绍了Springboot整合MybatisPlus的实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • java对象类型转换和多态性(实例讲解)

    java对象类型转换和多态性(实例讲解)

    下面小编就为大家带来一篇java对象类型转换和多态性(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • java内部类之成员内部类、局部内部类和匿名内部类用法及说明

    java内部类之成员内部类、局部内部类和匿名内部类用法及说明

    这篇文章主要介绍了java内部类之成员内部类、局部内部类和匿名内部类的用法及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 利用java判断字符串中有汉字 (常用正则表达式)

    利用java判断字符串中有汉字 (常用正则表达式)

    最近工作中遇到了一个需求,需要利用java获取字符串中的数字,尝试几种方法后发现利用正则表达式实现最为方便,这篇文章主要给大家介绍了关于如何利用java判断字符串中有汉字 的相关资料,文中还分享了一些常用正则表达式,需要的朋友可以参考下
    2023-10-10
  • springboot集成微软teams的实例代码

    springboot集成微软teams的实例代码

    Microsoft Teams 是一款基于聊天的智能团队协作工具,可以同步进行文档共享,并为成员提供包括语音、视频会议在内的即时通讯工具,今天给大家介绍springboot集成微软teams的示例代码,感兴趣的朋友一起看看吧
    2022-01-01
  • jpa使用注解生成表时无外键问题及解决

    jpa使用注解生成表时无外键问题及解决

    这篇文章主要介绍了jpa使用注解生成表时无外键问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • SpringBoot整合mybatis-plus进阶详细教程

    SpringBoot整合mybatis-plus进阶详细教程

    本文主要对mybatis-plus的条件构造器、AR模式、插件、逆向工程、自定义全局操作、公共字段自动填充等知识点进行讲解,需要的朋友参考下吧
    2021-09-09
  • Java class文件格式之属性_动力节点Java学院整理

    Java class文件格式之属性_动力节点Java学院整理

    在本文中, 主要讲解了class文件中的一些属性。 这些属性可以出现在class文件中的对个地方, 用来描述一些其他信息
    2017-06-06
  • Java注解Annotation原理及自定义注解代码实例

    Java注解Annotation原理及自定义注解代码实例

    这篇文章主要介绍了Java注解Annotation原理及自定义注解代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10

最新评论