1)查看当前服务器中的所有 topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --list
2)创建 topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --create --replication-factor 3 --partitions 1 -- topic first
选项说明:
--topic 定义 topic 名 --replication-factor 定义副本数 --partitions 定义分区数
3)删除 topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper |
hadoop102:2181 --delete --topic first |
需要 server.properties 中设置 delete.topic.enable=true 否则只是标记删除。
4)发送消息
[atguigu@hadoop102 kafka]$ bin/kafka-console-producer.sh --broker- list hadoop102:9092 --topic first
>hello world
>atguigu atguigu
5)消费消息
老版本用zookeeper,新版本用bootstrap,offset存储在本地
[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh \ --zookeeper hadoop102:2181 --topic first
[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh \ --bootstrap-server hadoop102:9092 --topic first
[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh \ --bootstrap-server hadoop102:9092 --from-beginning --topic first
6)查看某个 Topic 的详情
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --describe --topic first
7)修改分区数
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper |
hadoop102:2181 --alter --topic first --partitions 6 |