kafka命令行操作

目录

一、主题命令

二、生产者命令行操作

三、消费者命令行操作


kafka安装请看:http://t.csdn.cn/yjsMS


注意:

kafka版本:3.0.0

3.0.0的命令操作与3.0.0的命令不同。


一、主题命令

1.查看操作主题命令参数

bin/kafka-topics.sh

参数

描述

--bootstrap-server <String: server toconnect to>

连接的Kafka Broker主机名称和端口号。

--topic <String: topic>

操作的topic名称。

--create

创建主题。

--delete

删除主题。

--alter

修改主题。

--list

查看所有主题。

--describe

查看主题详细描述。

--partitions <Integer: # of partitions>

设置分区数。

--replication-factor<Integer: replication factor>

设置分区副本。

--config <String: name=value>

更新系统默认的配置。

2)查看当前服务器中的所有topic

[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server 192.168.xx.xxx:9092 --list

例子:bin/kafka-topics.sh --bootstrap-server liuyizhao01:9092 --list

3)创建first topic

[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server 192.168.xx.xxx:9092 --create --partitions 1 --replication-factor 3 --topic topic_name

例子:bin/kafka-topics.sh --bootstrap-server liuyizhao01:9092 --create --partitions 1 --replication-factor 3 --topic first

选项说明:

--topic 定义topic名

--replication-factor  定义副本数

--partitions  定义分区数

4)查看first主题的详情

[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server 192.168.xx.xxx:9092 --describe --topic topic_name

例子:[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server liuyizhao01:9092 --describe --topic first

5)修改分区数(注意:分区数只能增加,不能减少)

[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server 192.168.xx.xxx:9092 --alter --topic topic_name --partitions 3

例子:[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server liuyizhao01:9092 --alter --topic first --partitions 3

6)再次查看first主题的详情

[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server 192.168.xx.xxx:9092 --describe --topic topic_name

例子:[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server liuyizhao01:9092 --describe --topic first

7)删除topic

[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server 192.168.xx.xxx:9092 --delete --topic topic_name

例子:[root@liuyizhao01 kafka]# bin/kafka-topics.sh --bootstrap-server liuyizhao01:9092 --delete --topic first

二、生产者命令行操作

bin/kafka-console-producer.sh

参数

描述

--bootstrap-server <String: server toconnect to>

连接的Kafka Broker主机名称和端口号。

--topic <String: topic>

操作的topic名称。

2)发送消息

[root@liuyizhao01 kafka]# bin/kafka-console-producer.sh --bootstrap-server 192.168.xx.xxx:9092 --topic topic_name

例子:[root@liuyizhao01 kafka]# bin/kafka-console-producer.sh --bootstrap-server liuyizhao01:9092 --topic first

三、消费者命令行操作

 bin/kafka-console-consumer.sh

参数

描述

--bootstrap-server <String: server toconnect to>

连接的Kafka Broker主机名称和端口号。

--topic <String: topic>

操作的topic名称。

--from-beginning

从头开始消费。

--group <String: consumer group id>

指定消费者组名称。

1)消费first主题中的数据。

[root@liuyizhao01 kafka]# bin/kafka-console-consumer.sh --bootstrap-server 192.168.xx.xxx:9092 --topic topic_name

例子:[root@liuyizhao01 kafka]# bin/kafka-console-consumer.sh --bootstrap-server liuyizhao01 :9092 --topic first

2把主题中所有的数据都读取出来(包括历史数据)。

[root@liuyizhao01 kafka]# bin/kafka-console-consumer.sh --bootstrap-server 192.168.xx.xxx:9092 --from-beginning --topic topic_name

例子:[root@liuyizhao01 kafka]# bin/kafka-console-consumer.sh --bootstrap-server liuyizhao01 :9092 --from-beginning --topic first

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唯月作伴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值