目录

简介

操作

创建 --create

列表 --list

详细信息 --describe

删除 --delete

修改 --alter

基本参数


 

 

简介

kafka-topics.bat 脚本负责主题(topic)相关操作。通过kafka-run-class 调用kafka.admin.TopicCommand类。

 

 

操作

创建 --create

--create 创建

--zookeeper zookeeper地址

--replication-factor 副本数

--partitions 分区数量

因为该环境非集群环境,所以这里副本数设置为1。

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat --create --zookeeper localhost:2181 --topic Hello-Kafka --partitions 5 --replication-factor 1
Created topic Hello-Kafka.

创建完成后,可看到logs目录下生成了5个新的目录,命名规则为TopicsName-分区ID。

 

 

列表 --list

--list 查看

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat --list --zookeeper localhost:2181
Hello-Kafka
__consumer_offsets

 

详细信息 --describe

--describe 查看

--topic 指定查看哪个主题,如果不写该参数则会显示全部主题。

 

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat --zookeeper localhost:2181 --describe --topic Hello-Kafka
Topic:Hello-Kafka       PartitionCount:5        ReplicationFactor:1     Configs:
        Topic: Hello-Kafka      Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: Hello-Kafka      Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: Hello-Kafka      Partition: 2    Leader: 0       Replicas: 0     Isr: 0
        Topic: Hello-Kafka      Partition: 3    Leader: 0       Replicas: 0     Isr: 0
        Topic: Hello-Kafka      Partition: 4    Leader: 0       Replicas: 0     Isr: 0

PartitionCount:分区个数。
ReplicationFactor:副本个数。
Partition:分区编号,从 0 开始递增。
Leader:当前 分区起作用的 breaker.id。
Replicas: 当前副本数据所在的 breaker.id,是一个列表,排在最前面的其作用。
Isr:当前 kakfa 集群中可用的 breaker.id 列表。

 

 

删除 --delete

--delete 删除

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat --delete --topic Hello-Kafka --zookeeper localhost:2181
Topic Hello-Kafka is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

如果 delete.topic.enable = true

则会立刻删除掉该主题,否则只会标记为删除,并不会真正删除。需要到zk中删除该topic。

 

修改 --alter

 

 

新增分区

分区从5改为6

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat --alter --zookeeper localhost:2181 --topic Hello-Kafka --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!

 

减少分区 - 不可能

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat --alter --zookeeper localhost:2181 --topic Hello-Kafka --partitions 5
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Error while executing topic command : The number of partitions for a topic can only be increased. Topic Hello-Kafka currently has 6 partitions, 5 would not be an increase.
[2021-06-03 23:19:28,955] ERROR org.apache.kafka.common.errors.InvalidPartitionsException: The number of partitions for a topic can only be increased. Topic Hello-Kafka currently has 6 partitions, 5 would not be an increase.
 (kafka.admin.TopicCommand$)

 

 

 

基本参数

D:\DEV\MQ\kafka_2.12-2.3.0\kafka_2.12-2.3.0\bin\windows>kafka-topics.bat
Create, delete, describe, or change a topic.
Option                                   Description
------                                   -----------
--alter                                  Alter the number of partitions,
                                           replica assignment, and/or
                                           configuration for the topic.
--at-min-isr-partitions                  if set when describing topics, only
                                           show partitions whose isr count is
                                           equal to the configured minimum. Not
                                           supported with the --zookeeper
                                           option.
--bootstrap-server <String: server to    REQUIRED: The Kafka server to connect
  connect to>                              to. In case of providing this, a
                                           direct Zookeeper connection won't be
                                           required.
--command-config <String: command        Property file containing configs to be
  config property file>                    passed to Admin Client. This is used
                                           only with --bootstrap-server option
                                           for describing and altering broker
                                           configs.
--config <String: name=value>            A topic configuration override for the
                                           topic being created or altered.The
                                           following is a list of valid
                                           configurations:
                                                cleanup.policy
                                                compression.type
                                                delete.retention.ms
                                                file.delete.delay.ms
                                                flush.messages
                                                flush.ms
                                                follower.replication.throttled.
                                           replicas
                                                index.interval.bytes
                                                leader.replication.throttled.replicas
                                                max.message.bytes
                                                message.downconversion.enable
                                                message.format.version
                                                message.timestamp.difference.max.ms
                                                message.timestamp.type
                                                min.cleanable.dirty.ratio
                                                min.compaction.lag.ms
                                                min.insync.replicas
                                                preallocate
                                                retention.bytes
                                                retention.ms
                                                segment.bytes
                                                segment.index.bytes
                                                segment.jitter.ms
                                                segment.ms
                                                unclean.leader.election.enable
                                         See the Kafka documentation for full
                                           details on the topic configs.It is
                                           supported only in combination with --
                                           create if --bootstrap-server option
                                           is used.
--create                                 Create a new topic.
--delete                                 Delete a topic
--delete-config <String: name>           A topic configuration override to be
                                           removed for an existing topic (see
                                           the list of configurations under the
                                           --config option). Not supported with
                                           the --bootstrap-server option.
--describe                               List details for the given topics.
--disable-rack-aware                     Disable rack aware replica assignment
--exclude-internal                       exclude internal topics when running
                                           list or describe command. The
                                           internal topics will be listed by
                                           default
--force                                  Suppress console prompts
--help                                   Print usage information.
--if-exists                              if set when altering or deleting or
                                           describing topics, the action will
                                           only execute if the topic exists.
                                           Not supported with the --bootstrap-
                                           server option.
--if-not-exists                          if set when creating topics, the
                                           action will only execute if the
                                           topic does not already exist. Not
                                           supported with the --bootstrap-
                                           server option.
--list                                   List all available topics.
--partitions <Integer: # of partitions>  The number of partitions for the topic
                                           being created or altered (WARNING:
                                           If partitions are increased for a
                                           topic that has a key, the partition
                                           logic or ordering of the messages
                                           will be affected
--replica-assignment <String:            A list of manual partition-to-broker
  broker_id_for_part1_replica1 :           assignments for the topic being
  broker_id_for_part1_replica2 ,           created or altered.
  broker_id_for_part2_replica1 :
  broker_id_for_part2_replica2 , ...>
--replication-factor <Integer:           The replication factor for each
  replication factor>                      partition in the topic being created.
--topic <String: topic>                  The topic to create, alter, describe
                                           or delete. It also accepts a regular
                                           expression, except for --create
                                           option. Put topic name in double
                                           quotes and use the '\' prefix to
                                           escape regular expression symbols; e.
                                           g. "test\.topic".
--topics-with-overrides                  if set when describing topics, only
                                           show topics that have overridden
                                           configs
--unavailable-partitions                 if set when describing topics, only
                                           show partitions whose leader is not
                                           available
--under-min-isr-partitions               if set when describing topics, only
                                           show partitions whose isr count is
                                           less than the configured minimum.
                                           Not supported with the --zookeeper
                                           option.
--under-replicated-partitions            if set when describing topics, only
                                           show under replicated partitions
--version                                Display Kafka version.
--zookeeper <String: hosts>              DEPRECATED, The connection string for
                                           the zookeeper connection in the form
                                           host:port. Multiple hosts can be
                                           given to allow fail-over.

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐