文章目录
Kafka Server启动
$ bin/kafka-server-start.sh config/server.properties
生产、消费消息
1. 生产消息
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic <topic_name> --request-required-acks all
2. 消费消息
$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic <topic_name> --group <group_name> --from-beginning --consumer-property enable.auto.commit=false
如果不指定组名称(-group)的话,每次运行消费消息命令都会自动生成一个新的消费者组。
主题管理
创建topic
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic <topic_name> --replication-factor 3 --partitions 5
删除主题
bin/kafka-topics.sh --bootstrap-server broker_host:port --delete --topic <topic_name>
查看所有主题
bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
查看主题信息
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic <topic_name>
修改主题分区
bin/kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic <topic_name> --partitions <partition_num>
目前Kafka不允许减少某个主题的分区数,指定的分区数一定要比原有分区数大。
修改主题参数
bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name <topic_name> --alter --add-config max.message.bytes=10485760
分区重分配
bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --reassignment-json-file reassign.json --execute
修改主题限速
主题限速主要是设置Leader和Follower副本使用的带宽:
bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --add-config 'leader.replication.throttled.rate=104857600,follower.replication.throttled.rate=104857600' --entity-type brokers --entity-name 0
设置所有Leader所有副本和Follower所有副本的带宽使用
bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --add-config 'leader.replication.throttled.replicas=*,follower.replication.throttled.replicas=*' --entity-type topics --entity-name test
消费者信息
查看消费者位移
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group <group_name>
查看消费者组提交的位移数据
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic __consumer_offsets --formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter" --from-beginning
查看消费者组的状态信息
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic __consumer_offsets --formatter "kafka.coordinator.group.GroupMetadataManager\$GroupMetadataMessageFormatter" --from-beginning