说明
- 以下使用 zk 为 localhost-102:2181
- 使用的 kafka 为 localhost-102:9092
- 创建的 topic 名为 atguigu
查看当前服务器中的所有topic
bin/kafka-topics.sh --zookeeper localhost-102:2181 --list
创建topic
bin/kafka-topics.sh --zookeeper localhost-102:2181 \
--create --topic atguigu --replication-factor 3 --partitions 2
选项说明:
--topic 定义topic名
--partitions 定义分区数
--replication-factor 定义副本数
删除topic
bin/kafka-topics.sh --zookeeper localhost-102:2181 \
--delete --topic atguigu
发送消息
bin/kafka-console-producer.sh \
--broker-list localhost-102:9092 --topic atguigu
>hello world
>atguigu atguigu
消费消息
bin/kafka-console-consumer.sh \
--bootstrap-server localhost-102:9092 --from-beginning --topic atguigu
选项说明:
--from-beginning:会把主题中以往所有的数据都读取出来
查看某个Topic的详情
bin/kafka-topics.sh --zookeeper localhost-102:2181 \
--describe --topic atguigu
修改分区数
bin/kafka-topics.sh --zookeeper localhost-102:2181 \
--alter --topic atguigu --partitions 6