检查消费者位置
bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --describe --group test-consumer-group
列出所有topic中的所有用户组:
bin/kafka-consumer-groups.sh --bootstrap-server broker1:9092 --list
查看所有topic中的消费者组的偏移量:
bin/kafka-consumer-groups.sh --bootstrap-server broker1:9092 --describe --group test-consumer-group
启动
bin/kafka-server-start.sh config/server.properties &
创建topic(4个分区,2个副本)
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 4 --topic test
查询集群描述
bin/kafka-topics.sh --describe --zookeeper
生产者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
消费者
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test
新生产者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test --producer.config config/producer.properties
新消费者
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --new-consumer --from-beginning --consumer.config config/consumer.properties
消费者列表查询
bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --list
显示某个消费组的消费详情(仅支持offset存储在zookeeper上的)
bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zkconnect localhost:2181 --group test
显示某个消费组的消费详情
bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --describe --group test-consumer-group
平衡leader
bin/kafka-preferred-replica-election.sh --zookeeper zk_host:port/chroot
Kafka自带压测命令
bin/kafka-producer-perf-test.sh --topic test --num-records 100 --record-size 1 --throughput 100 --producer-props bootstrap.servers=localhost:9092
查看有哪些topic
bin/kafka-topics.sh --list --zookeeper 192.168.6.188:2181
除了手工创建topic外,也可以配置broker,当发布一个不存在的topic时自动创建topic。
查看某个topic的详细信息
bin/kafka-topics.sh --describe --zookeeper 192.168.6.188:2181 --topic test
发布消息
bin/kafka-console-producer.sh --broker-list 192.168.6.187:9092 --topic test
消费消息
bin/kafka-console-consumer.sh --zookeeper 192.168.6.188:2181 --topic test --from-beginning
使用Kafka Connect来导入/导出数据
bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties
提供3个配置文件作为参数。第一个始终是kafka Connect进程,如kafka broker连接和数据库序列化格式;第二个是导入连接器,从导入文件中读取并发布到Kafka主题;第三个是导出连接器,从kafka主题读取消息输出到外部文件
镜像集群之间的数据
bin/kafka-run-class.sh kafka.tools.MirrorMaker --consumer.config consumer-1.properties --consumer.config consumer-2.properties --producer.config producer.properties --whitelist my-topic
–whitelist 选项指定topic列表。此选项允许使用java风格的正则表达式。可以使用–whitelist ‘A|B’ ,A和B是镜像名。或者你可以镜像所有topic –whitelist ‘*’,为了确保引用的正则表达式不会被shell认为是一个文件路径,允许使用‘,’ 而不是’|’指定topic列表。
用–blacklist来排除哪些是不需要的,目前–new.consumer不支持。
镜像结合配置auto.create.topics.enable=true,这样副本集群就会自动创建和复制分区重分配
分区重新分配工具可以跨broker迁移分区,分区分配工具没有自动研究kafka集群的数据分布和迁移分区达到负载分布的能力,因此,管理员要弄清楚哪些topic或分区应该迁移。
分区分配工具的3种模式:
–generate: 生成分配规则的json文件,此选项,仅提供了一个方便的方式来生成特定的topic和目标broker列表的分区重新分配 “计划”。
–execute: 执行用–generate 生成的分配规则,(–reassignment-json-file 选项),可以自定义分配计划,也可以由管理员或通过–generate选项生成
–verify: 验证执行–execute重新分配的结果,列出所有分区的状态,状态可以是成功完成,失败或正在进行中的自动将数据迁移到新机器:
下面的例子将主题foo1,foo2的所有分区移动到新的broker 5,6。移动结束后,主题foo1和foo2所有的分区都会只会在broker 5,6。
注意:下面所有的json文件,都是要你自己新建的,不是自动创建的,需要你自己把生成的规则复制到你新建的json文件里,然后执行。
执行迁移工具需要接收一个json文件,首先需要你确认topic的迁移计划并创建json文件,如下所示cat topics-to-move.json {"topics": [{"topic": "foo1"},{"topic": "foo2"}], "version":1 }
一旦json准备好,使用分区重新分配工具生成一个“候选人”分配规则 :
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --topics-to-move-json-file topics-to-move.json --broker-list "5,6" --generate Current partition replica assignment
生成从主题foo1,foo2迁移所有的分区到broker 5,6的候选人分配规则。注意,这个时候,迁移还没有开始,它只是告诉你当前分配和新的分配规则,当前分配规则用来回滚,新的分配规则保存在json文件。然后,用–execute选项来执行它。
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file expand-cluster-reassignment.json --execute Current partition replica assignment
最后,–verify 选项用来检查parition重新分配的状态,注意, expand-cluster-reassignment.json(与–execute选项使用的相同)和–verify选项一起使用。
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file expand-cluster-reassignment.json --verify
自定义分区分配和迁移:
分区重新分配工具也可以有选择性将分区副本移动到指定的broker。当用这种方式,假定你已经知道了分区规则,不需要通过工具生成规则,可以跳过–generate,直接使用—execute
下面的例子是移动主题foo1的分区0到brokers 5,6 和主题foo2的分区1到broker 2,3。
第一步是,手工写一个自定义的分配计划到json文件中:cat custom-reassignment.json {"version":1,"partitions":[{"topic":"foo1","partition":0,"replicas":[5,6]},{"topic":"foo2","partition":1,"replicas":[2,3]}]}
然后,–execute 选项执行分配处理:
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file custom-reassignment.json --execute Current partition replica assignment
最后使用–verify 验证。
增加副本
在现有分区增加副本是很容易的,只要指定自定义的重新分配的json文件脚本,并用 –execute 选项去执行这个脚本。
增加副本之前,分区已存在的副本在broker5上,它也作为增加副本的一部分,我们将副本添加到broker6和7上。
第一步手工写一个自定义的分配的json脚本 :cat increase-replication-factor.json {"version":1, "partitions":[{"topic":"foo","partition":0,"replicas":[5,6,7]}]}
然后,用–execute选项运行json脚本。
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --execute Current partition replica assignment
– version 选项来验证parition分配的状态。注意,使用同样的 increase-replication-factor.json
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --verify
也可以使用kafka-topic工具验证:
bin/kafka-topics.sh --zookeeper localhost:2181 --topic foo --describe
彻底删除topic
①从zookeerer删除信息:/usr/local/kafka_2.10-0.8.1.1/bin/kafka-run-class.shkafka.admin.DeleteTopicCommand --zookeeper 10.12.0.91:2181,10.12.0.92:2181,10.12.0.93:2181/kafka--topic zitest2
成功后返回信息:deletion succeeded!
②JPS查看kill掉QuorumPeerMain和Kafka进程
③从log.dirs目录删除文件,可以看到多个子目录名字如zitest2-0,zitest2-1…zitest2-n(就是你topic的partition个数)rm –fr zitest2-0……zitest2-n
④修改日志目录的recovery-point-offset-checkpoint和replication-offset-checkpoint文件(要小心删除,否则待会kafka不能正常启动起来)
replication-offset-checkpoint格式如下:0 4(partition总数) zitest2 0 0 zitest2 3 0 hehe 0 0 hehe 1 0
修改后如下:
0 2(partition总数) hehe 0 0 hehe 1 0
把含有zitest2行全部去掉,并且把partition总数修改为减去zitest2的partition的剩余数目,同理recovery-point-offset-checkpoint也是这样修改。
完成后就可以正常启动zookeeper和kafka。数据迁移限制带宽
当执行重新平衡时,用下面的命令,它在移动分区时,将不会超过50MB/s:bin/kafka-reassign-partitions.sh --zookeeper myhost:2181--execute --reassignment-json-file bigger-cluster.json —throttle 50000000
如果想在重新平衡期间修改限制,增加吞吐量,以便完成的更快。可以重新运行execute命令,用相同的reassignment-json-file:
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --execute --reassignment-json-file bigger-cluster.json --throttle 700000000
一旦重新平衡完成,可以使用–verify操作验证重新平衡的状态。如果重新平衡已经完成,限制也会通过–verify命令移除。这点很重要,因为一旦重新平衡完成,并通过–veriry操作及时移除限制。否则可能会导致定期复制操作的流量也受到限制。
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --verify --reassignment-json-file bigger-cluster.json
设置配额:
默认情况下,客户端的配额不受限制。可以为每个user或client-id分组设置自定义的配额。
配置自定义的配额(user=user1,client-id=clientA):bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048' --entity-type users --entity-name user1 --entity-type clients --entity-name clientA
为user=user1配置自定义的配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048' --entity-type users --entity-name user1
为client-id=clientA配置自定义的配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048' --entity-type clients --entity-name clientA
可以通过–entity-default为(user,client-id),user或client-id group设置默认的配额。
为user=userA配置默认client-id配额:bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048' --entity-type users --entity-name user1 --entity-type clients --entity-default
为user配置默认配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048' --entity-type users --entity-default
为client-id配置默认配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048' --entity-type clients --entity-default
为指定的(user,client-id)展示配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type users --entity-name user1 --entity-type clients --entity-name clientA
为指定的user展示配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type users --entity-name user1
为指定的client-id展示配额:
bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type clients --entity-name clientA
如果没有指定名称,则展示指定的类型的,查看所有user:
bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type users
(user,client)也是一样:
bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type users --entity-type clients
可以通过在broker中设置以下配置来设置适用于所有client-id的默认配额。仅当未在Zookeeper中配置配额覆盖或默认值时,才应用这些属性。 默认情况下,每个client-id接收一个无限制的配额。 以下将每个producer和consumer client-id的默认配额设置为10MB /秒:
quota.producer.default=10485760 quota.consumer.default=10485760
请注意,这些属性已被弃用,可能会在将来的版本中删除。 请优先使用kafka-configs.sh。
- 使用Kafka Stream来处理数据
bin/kafka-run-class org.apache.kafka.streams.examples.wordcount.WordCountDemo
Kafka_Shell命令
最新推荐文章于 2024-08-19 17:57:56 发布