【Kafka基础】topic命令行工具kafka-topics.sh:基础操作命令解析

Kafka作为分布式流处理平台的核心组件,其主题管理是每个开发者必须掌握的关键技能。本文将详细解析kafka-topics.sh工具的使用技巧,从基础操作操作开始,助您轻松驾驭Kafka主题管理。

1 创建主题

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --create \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --partitions 3 \
    --replication-factor 2

参数说明

  • --create:创建新主题
  • --bootstrap-server:指定Kafka服务器地址(格式为host:port)
  • --topic:要创建的主题名称
  • --partitions:主题的分区数量(决定并行度)
  • --replication-factor:副本因子(决定数据冗余度)

    示例

    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --create \

    复制代码
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --partitions 3 \
    --replication-factor 2

    Created topic testtopic.
    [root@node6 bin]#

2 列出所有主题

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --list \
    --bootstrap-server 192.168.10.33:9092

参数说明

  • --list:列出集群中所有可用主题

    示例

    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --list \

    复制代码
    --bootstrap-server 192.168.10.33:9092

    __consumer_offsets
    test_topic
    testtopic
    [root@node6 bin]#

3 查看主题详情

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --describe \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic

参数说明

  • --describe:显示主题的详细信息
  • --topic:指定要查看的主题

    示例

    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --describe \

    复制代码
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic

    Topic: testtopic PartitionCount: 3 ReplicationFactor: 2 Configs: compression.type=producer,min.insync.replicas=2,segment.jitter.ms=30000,cleanup.policy=delete,segment.bytes=1073741824,max.message.bytes=10485760,unclean.leader.election.enable=false
    Topic: testtopic Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2
    Topic: testtopic Partition: 1 Leader: 2 Replicas: 2,3 Isr: 2,3
    Topic: testtopic Partition: 2 Leader: 3 Replicas: 3,1 Isr: 3,1
    [root@node6 bin]#

4 增加分区数

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --alter \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --partitions 4

参数说明

  • --alter:修改主题配置
  • --partitions:新的分区数(注意:只能增加不能减少)

    示例

    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --alter \

    复制代码
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --partitions 4

    [root@node6 bin]#
    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --describe --bootstrap-server 192.168.10.33:9092 --topic testtopic
    Topic: testtopic PartitionCount: 4 ReplicationFactor: 2 Configs: compression.type=producer,min.insync.replicas=2,segment.jitter.ms=30000,cleanup.policy=delete,segment.bytes=1073741824,max.message.bytes=10485760,unclean.leader.election.enable=false
    Topic: testtopic Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2
    Topic: testtopic Partition: 1 Leader: 2 Replicas: 2,3 Isr: 2,3
    Topic: testtopic Partition: 2 Leader: 3 Replicas: 3,1 Isr: 3,1
    Topic: testtopic Partition: 3 Leader: 1 Replicas: 1,3 Isr: 1,3
    [root@node6 bin]#

5 删除主题

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --delete \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic

参数说明

  • --delete:删除指定主题(需确保 delete.topic.enable=true)

    示例

    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --delete \

    复制代码
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic

    [root@node6 bin]# /export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --list \

    复制代码
    --bootstrap-server 192.168.10.33:9092

    __consumer_offsets
    test_topic
    [root@node6 bin]#

相关推荐
一路向北North6 小时前
使用reactor-rabbitmq库监听Rabbitmq
分布式·rabbitmq·ruby
Amy1870211182311 小时前
赋能低压分布式光伏“四可”建设,筑牢电网安全新防线
分布式
June bug15 小时前
【软考中级·软件评测师】下午题·面向对象测试之架构考点全析:分层、分布式、微内核与事件驱动
经验分享·分布式·职场和发展·架构·学习方法·测试·软考
阿波罗.201216 小时前
Zookeeper 客户端 .net访问框架 ZookeeperNetEx项目开发编译
分布式·zookeeper
Bug退退退12317 小时前
RabbitMQ 工作模式
java·分布式·rabbitmq
weixin_4383354017 小时前
分布式锁实现方式:基于Redis的分布式锁实现(Spring Boot + Redis)
数据库·redis·分布式
危险、1 天前
RabbitMQ 通过HTTP API删除队列命令
分布式·http·rabbitmq
周某某~1 天前
windows安装RabbitMQ
分布式·rabbitmq
Bug退退退1231 天前
RabbitMQ 高级特性之消息确认
java·分布式·rabbitmq
一只程序汪1 天前
【如何实现分布式压测中间件】
分布式·中间件