Kafka 常用命令行操作汇总

启动服务

bin/kafka-server-start.sh config/server.properties 阻塞进程
bin/kafka-server-start.sh -daemon config/server.properties 守护进程

停止服务

bin/kafka-server-stop.sh config/server.properties

新建Topic

bin/kafka-topics.sh --zookeeper centos7-1:2181 --create --topic mytopic --partitions 10 --replication-factor 3
bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic mytopic --partitions 10 --replication-factor 3


–zookeeper指定zookeeper的地址和端口,
–partitions指定partition的数量,
–replication-factor指定数据副本的数量。
也就是说,如果有100条数据,会被切分成10份,
每一份有三个副本,存放在不同的partition里。

删除Topic

bin/kafka-topics.sh --zookeeper centos7-1:2181 --delete --topic mytopic 
bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic mytopic 

修改Topic

bin/kafka-topics.sh --zookeeper centos7-1:2181 --alter --topic mytopic --partitions 20
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic mytopic --partitions 20

查看所有topic列表

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

查看指定topic明细

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

启动Producer(生产者)

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

启动Consumer(消费者)

bin/kafka-console-consumer.sh --bootstrap-server centos7-1:9092 --topic mytopic 
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytopic --from-beginning

如果加上 --from-beginning 指定从第一条数据开始消费
这里可以体现0.9版本以后kafka记录消费消息的偏移量在kafka本地了,已经删除zookeeper的选项了

猜你喜欢

转载自blog.csdn.net/weixin_46122692/article/details/109188321
今日推荐