kafka operation

运行环境:mac os

1. 启动zookeeper

./bin/zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties

2. 启动kafka服务

./bin/kafka-server-start /usr/local/etc/kafka/server.properties

3. 查看topic列表

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

4. 创建topic

./bin/kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic hezhixiong

--create:指定创建topic动作
--zookeeper:指定kafka连接zk的地址
--replication-factor:指定每个分区的复制因子个数,默认1个
--partitions:指定当前创建的kafka分区数量,默认为1个
--topic:指定新建topic的名称

5. 查看topic的描述信息

./bin/kafka-topics --describe --zookeeper localhost:2181 --topic hezhixiong

6. 修改topic信息

./bin/kafka-topics --zookeeper localhost:2181 --alter --topic hezhixiong --partitions 4
<!-- Kafka分区数量只允许增加,不允许减少 -->

7. 删除topic

./bin/kafka-topics --zookeeper localhost:2181 --delete --topic hezhixiong
Topic hezhixiong is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

默认情况下Kafka的Topic是没法直接删除的,而是标记删除而已。如果需要彻底删除topic,有以下两种方式:
1. 通过delete命令删除后,手动将本地磁盘以及zk上的相关topic的信息删除
2. 配置server.properties文件,给定参数delete.topic.enable=true,重启kafka服务,此时执行delete命令表示允许进行Topic的删除

8. 生产者

./bin/kafka-console-producer --broker-list localhost:9092 --topic hezhixiong

9. 消费者

./bin/kafka-console-consumer --bootstrap-server localhost:9092 --topic hezhixiong --from-beginning

猜你喜欢

转载自www.cnblogs.com/hezhixiong/p/10078994.html