【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]#

相关推荐
回家路上绕了弯1 小时前
深入解析Agent Subagent架构:原理、协同逻辑与实战落地指南
分布式·后端
初次攀爬者1 小时前
Kafka的Rebalance基础介绍
后端·kafka
初次攀爬者1 天前
Kafka + KRaft模式架构基础介绍
后端·kafka
初次攀爬者1 天前
Kafka + ZooKeeper架构基础介绍
后端·zookeeper·kafka
初次攀爬者1 天前
Kafka 基础介绍
spring boot·kafka·消息队列
DemonAvenger5 天前
Kafka性能调优:从参数配置到硬件选择的全方位指南
性能优化·kafka·消息队列
初次攀爬者5 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
yumgpkpm6 天前
AI视频生成:Wan 2.2(阿里通义万相)在华为昇腾下的部署?
人工智能·hadoop·elasticsearch·zookeeper·flink·kafka·cloudera
予枫的编程笔记6 天前
【Kafka高级篇】避开Kafka原生重试坑,Java业务端自建DLQ体系,让消息不丢失、不积压
java·kafka·死信队列·消息中间件·消息重试·dlq·java业务开发