消息中间件--04. Kafka常用的命令

启动zookeeper

1
2
$ bin/zkServer.sh start conf/zoo.cfg &
$ bin/zkCli.sh

启动Kafka

我们启动两个实例:

1
2
3
$ bin/kafka-server-start.sh -daemon config/server.properties
$ bin/kafka-server-start.sh -daemon config/server-1.properties

启动后,使用jps进程查看对应的kafka进程:
Kafka常用的命令

创建主题

现在我们来创建一个名字为testTopictestTopic1 的Topic,这个topic有两个partition,并且备份因子也设置为2:

1
2
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 2 --topic testTopic
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 2 --topic testTopic1

查看Kafka的实例

1
2
3
4
$ bin/zkCli.sh 
$ ls / #查看zk的根目录kafka相关节点
$ ls /brokers/ids #查看kafka节点

Kafka常用的命令

查看所有的topic

1
bin/kafka-topics.sh --list --zookeeper localhost:2181

Kafka常用的命令

除了我们通过手工的方式创建Topic,我们可以配置broker,当producer发布一个消息某个指定的Topic,但是这个Topic并不存在时,就自动创建。

查看partition的作用

1
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic testTopic

Kafka常用的命令

发送消息

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

消费消息

1
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092  --consumer-property group.id=testGroup --consumer-property client.id=consumer-1  --topic testTopic  

删除Topic

1
2
3

kafka-topics.sh --zookeeper localhost:2181 --delete --topic <topic_name>