# 常用命令

# Topic

注意 --zookeeper big002:2181 后面需不需要带 /kafka

# 查看 topic 列表
$ bin/kafka-topics.sh --zookeeper big002:2181 --list

# 新建 topic:名称是 myTopic,副本数=3,分区数=1
$ bin/kafka-topics.sh --zookeeper big002:2181 --create --replication-factor 1 --partitions 1 --topic myTopic

# 查看某个 Topic 的详情
$ /usr/local/soft/kafka_2.13-2.7.0/bin/kafka-topics.sh --zookeeper big002:2181 --describe --topic myTopic
Topic: myTopic	PartitionCount: 1	ReplicationFactor: 3	Configs: 
	Topic: myTopic	Partition: 0	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1

# 修改分区数
$ /usr/local/soft/kafka_2.13-2.7.0/bin/kafka-topics.sh --zookeeper big002:2181 --alter --topic myTopic --partitions 6

$ /usr/local/soft/kafka_2.13-2.7.0/bin/kafka-topics.sh --zookeeper big002:2181 --describe --topic myTopic
Topic: myTopic	PartitionCount: 6	ReplicationFactor: 3	Configs: 
	Topic: myTopic	Partition: 0	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1
	Topic: myTopic	Partition: 1	Leader: 3	Replicas: 3,2,1	Isr: 3,2,1
	Topic: myTopic	Partition: 2	Leader: 1	Replicas: 1,3,2	Isr: 1,3,2
	Topic: myTopic	Partition: 3	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1
	Topic: myTopic	Partition: 4	Leader: 3	Replicas: 3,1,2	Isr: 3,1,2
	Topic: myTopic	Partition: 5	Leader: 1	Replicas: 1,2,3	Isr: 1,2,3

# 删除 topic
# 需要 server.properties 中设置 delete.topic.enable=true 否则只是标记删除。
$ /usr/local/soft/kafka_2.13-2.7.0/bin/kafka-topics.sh --zookeeper big002:2181 --delete --topic myTopic

Topic myTopic is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
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

# 收发消息

## 消费消息
$ /usr/local/soft/kafka_2.13-2.7.0/bin/kafka-console-consumer.sh --topic myTopic --from-beginning --bootstrap-server big002:9092 

## 发送消息
$ /usr/local/soft/kafka_2.13-2.7.0/bin/kafka-console-producer.sh --topic myTopic --bootstrap-server big002:9092
1
2
3
4
5
更新时间: 2021-06-07 15:34:00