kafka相关命令整理

1、增加kafka分区

./kafka-topics.sh --zookeeper 192.168.2.70 --alter  --topic us_forward  --partitions 24

2、查看kafka的topic

./kafka-topics.sh --zookeeper 192.168.2.70 --list

3、查看单个topic的分区,也可以不加topic

./kafka-topics.sh --zookeeper 192.168.2.70 --describe --topic us_general

Topic: us_general Partition: 0 Leader: 117 Replicas: 117 Isr: 117

Topic: us_general Partition: 1 Leader: 117 Replicas: 117 Isr: 117

4、查看kafka各个组的偏移量

./kafka-run-class.sh  kafka.tools.ConsumerOffsetChecker --zookeeper 192.168.110.165:2181 -group test_groupid

5、启动kafka

bin/kafka-server-start.sh config/server.properties &

6、创建topic

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 24 --topic topic_test

7、发送消息

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic page_visits

8、消费消息

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic page_visits --from-beginning

9、删除topic

bin/kafka-topics.sh --zookeeper zk_host:port --delete --topic my_topic_name

10、重新平衡分区和领导者

./kafka-preferred-replica-election.sh --zookeeper 192.168.2.70

11、扩展集群

1)、先把要转移的topic写成json格式topics-to-move.json

{"topics": [

{"topic": "SYNC_DATABASE_UPDATE"},

{"topic": "SYNC_REALTIME_ALARM"},

{"topic": "SYNC_STATION_INFO"},

{"topic": "SYNC_VEHICLE_REG"},

{"topic": "ds_ctrlreq"},

{"topic": "us_ctrlrsp"},

{"topic": "us_forward"},

{"topic": "us_general"},

{"topic": "us_packet"}

],

"version":1

}

2)、用脚本生成一个自动分区的计划

./kafka-reassign-partitions.sh --zookeeper 192.168.2.70 --generate --topics-to-move-json-file topics-to-move.json  --broker-list 1,2,3 

3)、把生成的建议分区保存成json  topics-to-move-execute.json

4)、执行分区计划

/kafka-reassign-partitions.sh --zookeeper 192.168.2.70 --reassignment-json-file topics-to-move-execute.json --execute

5)、检查集群扩展是否完成

/kafka-reassign-partitions.sh --zookeeper 192.168.2.70 --reassignment-json-file topics-to-move-execute.json --verify

12、增加副本数

1)、编写扩展文件,可以利用扩展集群的方式导出现在的分布然后做修改

{ "version":1,

"partitions":[{"topic":"foo","partition":0,"replicas":[5,6,7]}]

}

2)、执行分区计划

/kafka-reassign-partitions.sh --zookeeper 192.168.2.70 --reassignment-json-file topics-to-move-execute.json --execute

3)、检查集群扩展是否完成

/kafka-reassign-partitions.sh --zookeeper 192.168.2.70 --reassignment-json-file topics-to-move-execute.json --verify