当前位置:   article > 正文

Kafka命令大全_kafka版本查看命令

kafka版本查看命令

kafka 脚本

  1. connect-distributed.sh
  2. connect-mirror-maker.sh
  3. connect-standalone.sh
  4. kafka-acls.sh
  5. kafka-broker-api-versions.sh
  6. kafka-configs.sh
  7. kafka-console-consumer.sh
  8. kafka-console-producer.sh
  9. kafka-consumer-groups.sh
  10. kafka-consumer-perf-test.sh
  11. kafka-delegation-tokens.sh
  12. kafka-delete-records.sh
  13. kafka-dump-log.sh
  14. kafka-leader-election.sh
  15. kafka-log-dirs.sh
  16. kafka-mirror-maker.sh
  17. kafka-preferred-replica-election.sh
  18. kafka-producer-perf-test.sh
  19. kafka-reassign-partitions.sh
  20. kafka-replica-verification.sh
  21. kafka-run-class.sh
  22. kafka-server-start.sh
  23. kafka-server-stop.sh
  24. kafka-streams-application-reset.sh
  25. kafka-topics.sh
  26. kafka-verifiable-consumer.sh
  27. kafka-verifiable-producer.sh
  28. trogdor.sh
  29. windows
  30. zookeeper-security-migration.sh
  31. zookeeper-server-start.sh
  32. zookeeper-server-stop.sh
  33. zookeeper-shell.sh

集群管理

  1. # 启动zookeeper
  2. bin/zookeeper-server-start.sh config/zookeeper.properties &
  3. # 停止zookeeper
  4. bin/zookeeper-server-stop.sh
  5. # 前台启动broker Ctrl + C 关闭
  6. bin/kafka-server-start.sh <path>/server.properties
  7. # 后台启动broker
  8. bin/kafka-server-start.sh -daemon <path>/server.properties
  9. # 关闭broker
  10. bin/kafka-server-stop.sh

topic相关

  1. # 创建topic(4个分区,2个副本)
  2. bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 4 --topic test
  3. # kafka版本 >= 2.2
  4. bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
  5. --create:指定创建topic动作
  6. --topic:指定新建topic的名称
  7. --zookeeper: 指定kafka连接zk的连接url,该值和server.properties文件中的配置项{zookeeper.connect}一样
  8. --partitions:指定当前创建的kafka分区数量,默认为1
  9. --replication-factor:指定每个分区的复制因子个数,默认1
  10. # 分区扩容,注意:分区数量只能增加,不能减少
  11. # kafka版本 < 2.2
  12. bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic1 --partitions 2
  13. # kafka版本 >= 2.2
  14. bin/kafka-topics.sh --bootstrap-server broker_host:port --alter --topic topic1 --partitions 2
  15. # 删除topic
  16. bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test
  17. # 查询topic列表
  18. bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --list
  19. # 查询topic列表(支持0.9版本+
  20. bin/kafka-topics.sh --list --bootstrap-server localhost:9092
  21. # 查看所有topic的详细信息
  22. bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --describe
  23. # 查询topic详情
  24. bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topicname

消费者组

查看consumer group列表有新、旧两种命令,分别查看新版(信息保存在broker中)consumer列表和老版(信息保存在zookeeper中)consumer列表,因而需要区分指定bootstrap–server和zookeeper参数

  1. # 新消费者列表查询(支持0.9版本+
  2. bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --list
  3. # 消费者列表查询(支持0.10版本+
  4. bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
  5. # 显示某个消费组的消费详情(仅支持offset存储在zookeeper上的)
  6. bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zookeeper localhost:2181 --group test
  7. # 显示某个新消费组的消费详情(0.9版本 - 0.10.1.0 之前)
  8. bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --describe --group my-group
  9. ## 显示某个消费组的消费详情(0.10.1.0版本+
  10. bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group my-group
  11. # 重设消费者组位移
  12. # 最早处
  13. bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group groupname --reset-offsets --all-topics --to-earliest --execute
  14. # 最新处
  15. bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group groupname --reset-offsets --all-topics --to-latest --execute
  16. # 某个位置
  17. bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group groupname --reset-offsets --all-topics --to-offset 2000 --execute
  18. # 调整到某个时间之后的最早位移
  19. bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group groupname --reset-offsets --all-topics --to-datetime 2019-09-15T00:00:00.000
  20. # 删除消费者组
  21. bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --delete --group groupname

显示某个消费组的消费详情
显示某个消费组的消费详情
各字段含义如下

TOPICPARTITIONCURRENT-OFFSETLOG-END-OFFSETLAGCONSUMER-IDHOSTCLIENT-ID
topic名字分区id当前已消费的条数总条数未消费的条数消费id主机ip客户端id

发送和消费

  1. # 生产者
  2. bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
  3. # 消费者,其中"--from-beginning"为可选参数,表示要从头消费消息
  4. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topicname --from-beginning
  5. # 指定groupid
  6. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topicname --from-beginning --consumer-property group.id=old-consumer-group
  7. # 指定分区
  8. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topicname --from-beginning --partition 0
  9. # 新生产者(支持0.9版本+
  10. bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test --producer.config config/producer.properties
  11. # 新消费者(支持0.9版本+
  12. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --new-consumer --from-beginning --consumer.config config/consumer.properties
  13. # kafka-verifiable-consumer.sh(消费者事件,例如:offset提交等)
  14. bin/kafka-verifiable-consumer.sh --broker-list localhost:9092 --topic test --group-id groupName
  15. # 高级点的用法
  16. bin/kafka-simple-consumer-shell.sh --brist localhost:9092 --topic test --partition 0 --offset 1234 --max-messages 10

切换leader

  1. # kafka版本 <= 2.4
  2. bin/kafka-preferred-replica-election.sh --zookeeper zk_host:port/chroot
  3. # kafka新版本
  4. bin/kafka-preferred-replica-election.sh --bootstrap-server broker_host:port

kafka自带压测命令

bin/kafka-producer-perf-test.sh --topic test --num-records 100 --record-size 1 --throughput 100  --producer-props bootstrap.servers=localhost:9092

kafka持续发送消息

持续发送消息到指定的topic中,且每条发送的消息都会有响应信息:

kafka-verifiable-producer.sh --broker-list $(hostname -i):9092 --topic test --max-messages 100000

zookeeper-shell.sh

如果kafka集群的zk配置了chroot路径,那么需要加上/path

  1. bin/zookeeper-shell.sh localhost:2181[/path]
  2. ls /brokers/ids
  3. get /brokers/ids/0

迁移分区

  1. 创建规则json
  1. cat > increase-replication-factor.json <<EOF
  2. {"version":1, "partitions":[
  3. {"topic":"__consumer_offsets","partition":0,"replicas":[0,1]},
  4. {"topic":"__consumer_offsets","partition":1,"replicas":[0,1]},
  5. {"topic":"__consumer_offsets","partition":2,"replicas":[0,1]},
  6. {"topic":"__consumer_offsets","partition":3,"replicas":[0,1]},
  7. {"topic":"__consumer_offsets","partition":4,"replicas":[0,1]},
  8. {"topic":"__consumer_offsets","partition":5,"replicas":[0,1]},
  9. {"topic":"__consumer_offsets","partition":6,"replicas":[0,1]},
  10. {"topic":"__consumer_offsets","partition":7,"replicas":[0,1]},
  11. {"topic":"__consumer_offsets","partition":8,"replicas":[0,1]},
  12. {"topic":"__consumer_offsets","partition":9,"replicas":[0,1]},
  13. {"topic":"__consumer_offsets","partition":10,"replicas":[0,1]},
  14. {"topic":"__consumer_offsets","partition":11,"replicas":[0,1]},
  15. {"topic":"__consumer_offsets","partition":12,"replicas":[0,1]},
  16. {"topic":"__consumer_offsets","partition":13,"replicas":[0,1]},
  17. {"topic":"__consumer_offsets","partition":14,"replicas":[0,1]},
  18. {"topic":"__consumer_offsets","partition":15,"replicas":[0,1]},
  19. {"topic":"__consumer_offsets","partition":16,"replicas":[0,1]},
  20. {"topic":"__consumer_offsets","partition":17,"replicas":[0,1]},
  21. {"topic":"__consumer_offsets","partition":18,"replicas":[0,1]},
  22. {"topic":"__consumer_offsets","partition":19,"replicas":[0,1]},
  23. {"topic":"__consumer_offsets","partition":20,"replicas":[0,1]},
  24. {"topic":"__consumer_offsets","partition":21,"replicas":[0,1]},
  25. {"topic":"__consumer_offsets","partition":22,"replicas":[0,1]},
  26. {"topic":"__consumer_offsets","partition":23,"replicas":[0,1]},
  27. {"topic":"__consumer_offsets","partition":24,"replicas":[0,1]},
  28. {"topic":"__consumer_offsets","partition":25,"replicas":[0,1]},
  29. {"topic":"__consumer_offsets","partition":26,"replicas":[0,1]},
  30. {"topic":"__consumer_offsets","partition":27,"replicas":[0,1]},
  31. {"topic":"__consumer_offsets","partition":28,"replicas":[0,1]},
  32. {"topic":"__consumer_offsets","partition":29,"replicas":[0,1]},
  33. {"topic":"__consumer_offsets","partition":30,"replicas":[0,1]},
  34. {"topic":"__consumer_offsets","partition":31,"replicas":[0,1]},
  35. {"topic":"__consumer_offsets","partition":32,"replicas":[0,1]},
  36. {"topic":"__consumer_offsets","partition":33,"replicas":[0,1]},
  37. {"topic":"__consumer_offsets","partition":34,"replicas":[0,1]},
  38. {"topic":"__consumer_offsets","partition":35,"replicas":[0,1]},
  39. {"topic":"__consumer_offsets","partition":36,"replicas":[0,1]},
  40. {"topic":"__consumer_offsets","partition":37,"replicas":[0,1]},
  41. {"topic":"__consumer_offsets","partition":38,"replicas":[0,1]},
  42. {"topic":"__consumer_offsets","partition":39,"replicas":[0,1]},
  43. {"topic":"__consumer_offsets","partition":40,"replicas":[0,1]},
  44. {"topic":"__consumer_offsets","partition":41,"replicas":[0,1]},
  45. {"topic":"__consumer_offsets","partition":42,"replicas":[0,1]},
  46. {"topic":"__consumer_offsets","partition":43,"replicas":[0,1]},
  47. {"topic":"__consumer_offsets","partition":44,"replicas":[0,1]},
  48. {"topic":"__consumer_offsets","partition":45,"replicas":[0,1]},
  49. {"topic":"__consumer_offsets","partition":46,"replicas":[0,1]},
  50. {"topic":"__consumer_offsets","partition":47,"replicas":[0,1]},
  51. {"topic":"__consumer_offsets","partition":48,"replicas":[0,1]},
  52. {"topic":"__consumer_offsets","partition":49,"replicas":[0,1]}]
  53. }
  54. EOF
  1. 执行
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --execute
  1. 验证
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --verify

MirrorMaker 跨机房灾备工具

bin/kafka-mirror-maker.sh --consumer.config consumer.properties --producer.config producer.properties --whitelist topicA|topicB

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/690513
推荐阅读
相关标签
  

闽ICP备14008679号