0%

Kafka命令行工具

一、Topic

1.1、创建

./kafka-topics.sh --bootstrap-server kafka01:9092,kafka02:9092,kafka03:9092 --create --topic topic01 --partitions 3 --replication-factor 3

1.2、查看

./kafka-topics.sh --bootstrap-server kafka01:9092,kafka02:9092,kafka03:9092 --list

1.3、详情

./kafka-topics.sh --bootstrap-server kafka01:9092,kafka02:9092,kafka03:9092 --describe --topic topic01

1.4、修改分区

分区只能增加,不能减少

./kafka-topics.sh --bootstrap-server kafka01:9092,kafka02:9092,kafka03:9092 --alter --topic topic01 --partitions 4

1.5、删除

./kafka-topics.sh --bootstrap-server kafka01:9092,kafka02:9092,kafka03:9092 --delete --topic topic01

二、zk中存储的元数据信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[zk: localhost:2181(CONNECTED) 14] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]

[zk: localhost:2181(CONNECTED) 18] ls -R /admin
/admin
/admin/delete_topics

[zk: localhost:2181(CONNECTED) 15] ls -R /brokers
/brokers
/brokers/ids
/brokers/seqid
/brokers/topics
/brokers/ids/0
/brokers/ids/1
/brokers/ids/2
/brokers/topics/topic01
/brokers/topics/topic01/partitions
/brokers/topics/topic01/partitions/0
/brokers/topics/topic01/partitions/1
/brokers/topics/topic01/partitions/2
/brokers/topics/topic01/partitions/0/state
/brokers/topics/topic01/partitions/1/state
/brokers/topics/topic01/partitions/2/state

[zk: localhost:2181(CONNECTED) 16] get /brokers/topics/topic01/partitions/0/state
{"controller_epoch":1,"leader":2,"version":1,"leader_epoch":0,"isr":[2,1,0]}

[zk: localhost:2181(CONNECTED) 17] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka01:9092"],"jmx_port":-1,"features":{},"host":"kafka01","timestamp":"1672065860261","port":9092,"version":5}

三、发送消息

3.1、生产者

./kafka-console-producer.sh --broker-list kafka01:9092,kafka02:9092,kafka03:9092 --topic topic01

3.2、消费者

./kafka-console-consumer.sh --bootstrap-server kafka01:9092,kafka02:9092,kafka03:9092 --topic topic01 --group g1 --property print.key=true --property print.value=true --property key.separator=,

当启动的多个消费者都指定--group g1的情况下,相同的消费组中只会有一个消费者消费消息