kafka 常用命令搜集整理 —— 筑梦之路

topic 相关
创建 topic:创建一个名为 test 的 topic,副本数为 1,分片数为 3

$ bin/kafka-topics.sh --zookeeper localhost:2181 --create --replication-factor 1 --partitions 3 --topic test
查看 topic 列表

$ bin/kafka-topics.sh --list --zookeeper localhost:2181
查看指定 topic 的配置项

$ bin/kafka-configs.sh --zookeeper localhost:2181 --entity-type topics --entity-name test
查看指定 Topic 的详细信息:包括 Partition 个数、副本数、ISR 信息

$ bin/kafka-topics.sh --zookeeper localhost:2181 --describe test
修改指定 Topic 的 Partition 个数。注意:该命令只能增加 Partition 的个数,不能减少 Partition 个数,当修改的 Partition 个数小于当前的个数,会报如下错误:Error while executing topic command : The number of partitions for a topic can only be increased

$ bin/kafka-topics.sh --alter --zookeeper localhost:2181 --topic test --partition 4
删除名字为 "test" 的 Topic。注意如果直接执行该命令,而没有设置 delete.topic.enable 属性为 true 时,是不会立即执行删除操作的,而是仅仅将指定的 Topic 标记为删除状态,之后会启动后台删除线程来删除。

$ bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic test
consumer group 相关
查看消费端的所有消费组信息(ZK 维护消费信息)

$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
查看消费端的所有消费组信息(Kafka 维护消费信息)

$ bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --list
查看指定 group 的详细消费情况,包括当前消费的进度,消息积压量等等信息(ZK 维护消息信息)

$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group console-consumer-1291 --describe
查看指定 group 的详细消费情况,包括当前消费的进度,消息积压量等等信息(Kafka 维护消费信息)

$ bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --group my_topic
consumer offset 相关
需要先确认 topic 和 consumer group。

查看指定 group 的 OffSet 信息、所有者等信息

$ bin/kafka-consumer-offset-checker.sh --zookeeper localhost:2181 --topic my_topic --group groupname
查看指定 group 的 OffSet 信息,包括消费者所在的 IP 信息等等

$ bin/kafka-consumer-offset-checker.sh --zookeeper localhost:2181 --topic mytopic --group groupname
config:修改属性
对指定 entry 添加配置,其中 entry 可以是 topics、clients;如下,给指定的 topic 添加两个属性,分别为 max.message.byte、flush.message

$ bin/kafka-configs.sh --zookeeper localhost:2181 --alter --entity-type topics --entity-name mytopic --add-config 'max.message.bytes=50000000,flush.message=5'
对指定 entry 删除配置,其中 entry 可以是 topics、clients;如下。对指定的 topic 删除 flush.message 属性。

$ bin/kafka-configs.sh --zookeeper localhost:2181 --alter --entity-type topics --entity-name mytopic --delete-config 'flush.message'
partition 相关
kafka-reassign-partitions.sh 脚本相关常用命令,主要操作 Partition 的负载情况。

生成 partition 的移动计划,--broker-list 参数指定要挪动的 Broker 的范围,--topics-to-move-json-file 参数指定 Json 配置文件

$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --broker-list "0" --topics-to-move-json-file ~/json/op_log_topic-to-move.json --generate
执行 Partition 的移动计划,--reassignment-json-file 参数指定 RePartition 后 Assigned Replica 的分布情况。

$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file ~/json/op_log_reassignment.json --execute
检查当前 rePartition 的进度情况。

$ bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file ~/json/op_log_reassignment.json
kafka 常见重要配置说明,分为四部分

Broker Config:kafka 服务端的配置
Producer Config:生产端的配置
Consumer Config:消费端的配置
Kafka Connect Config:kafka 连接相关的配置

猜你喜欢

转载自blog.csdn.net/qq_34777982/article/details/130422482