kafka-topics.sh脚本详解
通过脚本进行主题的管理,包括:创建主题、查看主题、修改主题、删除主题等操作。内部是靠kafka.admin.TopicCommand
接收参数运行。
[xuhaixing@xhx151 ~]$ kafka-topics.sh --help This tool helps to 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.compaction.lag.ms 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 (the kafka-configs CLI supports altering topic configs with a --bootstrap-server option). --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. --if-not-exists if set when creating topics, the action will only execute if the topic does not already exist. --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). If not supplied for create, defaults to the cluster default. --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. If not supplied, defaults to the cluster default. --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. 复制代码
kafka-topics.sh脚本中的参数
参数名称 | 释义 |
---|---|
alter | 用于修改主题 |
config 键值对 | 创建或修改主题时,用于设置主题级别的参数 |
create | 创建主题 |
delete | 删除主题 |
delete-config 配置名称 | 删除主题级别被覆盖的配置 |
describe | 查看主题的详细信息 |
disable-rack-aware | 创建主题时不考虑机架信息 |
help | 打印帮助信息 |
if-exists | 修改或删除主题时使用,只有当主题存在时才会执行动作 |
if-not-exists | 创建主题时使用,只有主题不存在时才会执行动作 |
list | 列出所有可用的主题 |
partitons 分区数 | 创建主题或增加分区时指定的分区数 |
replica-assignment 分配方案 | 手工指定分区副本分配方案 |
replication-factor 副本数 | 创建主题时指定副本因子 |
topic 主题名称 | 指定主题名称 |
topics-with-overrides | 使用describe查看主题信息时,只展示包含覆盖配置的主题 |
unavailable-partitions | 使用describe查看主题信息时,只展示包含没有leader副本的分区 |
under-replicated-partitions | 使用describe查看主题信息时,只展示包含失效副本的分区 |
zookeeper | 指定连接的zk的地址,已经过时,可以改成 --bootstrap-server |
1. 常用命令
查看所有topic
kafka-topics.sh --zookeeper 192.168.94.151:2181/kafka --list kafka-topics.sh --bootstrap-server 192.168.94.151:9092 --list复制代码
创建topic
kafka-topics.sh --zookeeper 192.168.94.151:2181/kafka --create --topic test-topic --replication-factor 2 --partitions 3 kafka-topics.sh --bootstrap-server 192.168.94.151:9092 --create --topic test-topic-2 --replication-factor 2 --partitions 3 --config retention.ms=36000000000 --config max.message.bytes=64000复制代码
查看topic配置信息
kafka-topics.sh --zookeeper 192.168.94.151:2181/kafka --describe --topic test-topic复制代码
扩大分区
kafka-topics.sh --zookeeper 192.168.94.151:2181/kafka --alter --topic test-topic --partitions 6复制代码
修改topic配置
kafka-topics.sh --bootstrap-server 192.168.94.151:9092 -topic test-topic --alter --config retention.ms=259200000复制代码
kafka topic config参数
cleanup.policy
过期或达到日志上限的清理策略(delete-删除;compact-压缩)默认值为delete
compression.type
指定给该topic最终的压缩类型(uncompressed;snappy;lz4;gzip;producer)默认值为producer
delete.retention.ms
压缩日志保留的最长时间,也是消费端消息的最长时间。单位为毫秒(默认值:86400000)
file.delete.delay.ms
从文件系统中删除前所等待的时间(默认值:60000)
flush.messages
在消息刷到磁盘前,日志分区收集的消息数(默认值:Long.MAX_VALUE)
flush.ms
消息刷到磁盘前,保存在内存中的最长时间,单位ms(默认值:Long.MAX_VALUE)
index.interval.bytes
当执行fetch操作后,需要一定的空间来扫描最近的offset大小。设置越大,扫描速度更快但更耗内存,一般情况下不用设置此参数。(默认值:4096)
max.message.bytes
log中能够容纳消息的最大字节数(默认值:1000012)
min.cleanable.dirty.ratio
日志清理的频率控制,越大清理更高效(默认值:0.5)
retention.bytes
topic每个分区的最大文件大小。(默认值:-1没有限制)
retention.ms
日志文件保留的ms。数据存储的最大时间超过这个时间会根据cleanup.policy策略处理数据。
segment.bytes
topic的文件是以segment文件存储的,该参数控制每个segment文件的大小(默认值:1073741824)
segment.index.bytes
对于segment日志的索引文件大小限制(默认值:10M)
作者:程序员星星toC
链接:https://juejin.cn/post/7035070113707458573