Kafka入门- 基础命令操作指南

发布于:2025-06-07 ⋅ 阅读:(19) ⋅ 点赞:(0)

基础命令

主题

参数 含义
–bootstrap-server 连接的Broker主机名称以及端口号
–topic 操作的topic
–create 创建主题
–delete 删除主题
–alter 修改主题
–list 查看所有主题
–describe 查看主题的详细描述
–partitions 设置分区数
–replication-factor 设置分区副本
–config 更新系统默认的配置

在执行查看命令时

bin/kafka-topics.sh --bootstrap-server 192.168.27.101:9092 --list

显示以下报错信息

[2023-07-18 16:19:18,101] WARN [AdminClient clientId=adminclient-1] Connection to node -1 (/192.168.27.101:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

解决方案是修改server.properties,将下面的修改为自己的ip+端口

#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.27.101:9092

新建一个topic,topic名称为first,设置分区数为1,分区副本为3(命令行不能修改分区副本的数量)

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

当命令正常执行之后,能够在zookeeper中查看到刚刚创建的kafka,在路径下有brokers:0,1,2,这就是设置的id。topics下的first就是刚刚创建的topic

在这里插入图片描述

命令查看topic的详细描述

[root@centos101 kafka_2.12-3.0.0]# bin/kafka-topics.sh --bootstrap-server 192.168.27.101:9092 --topic first --describe
Topic: first	TopicId: nM9fRCkARa2n2Ifgu37NnQ	PartitionCount: 1	ReplicationFactor: 3	Configs: segment.bytes=1073741824
	Topic: first	Partition: 0	Leader: 2	Replicas: 2,1,0	Isr: 2,1,P,1,0	Isr: 2,1,0

修改分区Partition数量

bin/kafka-topics.sh --bootstrap-server 192.168.27.101:9092 --topic first --alter --partitions 3

修改完成之后再次查看topic的详细描述

[root@centos101 kafka_2.12-3.0.0]# bin/kafka-topics.sh --bootstrap-server 192.168.27.101:9092 --topic first --describe
Topic: first	TopicId: nM9fRCkARa2n2Ifgu37NnQ	PartitionCount: 3	ReplicationFactor: 3	Configs: segment.bytes=1073741824
	Topic: first	Partition: 0	Leader: 2	Replicas: 2,1,0	Isr: 2,1,0
	Topic: first	Partition: 1	Leader: 0	Replicas: 0,1,2	Isr: 0,1,2
	Topic: first	Partition: 2	Leader: 1	Replicas: 1,2,0	Isr: 1,2,0

可以看到分区数量的增加,但是注意,分区数量增加后不能再减少比如修改分区为1

[root@centos101 kafka_2.12-3.0.0]# bin/kafka-topics.sh --bootstrap-server 192.168.27.101:9092 --topic first --alter --partitions 1
Error while executing topic command : Topic currently has 3 partitions, which is higher than the requested 1.
[2023-07-19 09:36:19,679] ERROR org.apache.kafka.common.errors.InvalidPartitionsException: Topic currently has 3 partitions, which is higher than the requested 1.
 (kafka.admin.TopicCommand$)

生产者

连接上主题,当出现回车之后就可以发送消息

bin/kafka-console-producer.sh --bootstrap-server 192.168.27.101:9092 --topic first

消费者

连接上主题,接收消费者连接上之后发送的消息

bin/kafka-console-consumer.sh --bootstrap-server 192.168.27.101:9092 --topic first

如果需要接收消费者连接上之前发送的所有消息,则在后面加上–from-beginning

bin/kafka-console-consumer.sh --bootstrap-server 192.168.27.101:9092 --topic first --from-beginning

生产与消费

当生产者和消费者都连接上了同一个topic之后,模拟生产者发送消息

[root@centos101 kafka_2.12-3.0.0]# bin/kafka-console-producer.sh --bootstrap-server 192.168.27.101:9092 --topic first
>nihao,kafka

此时消费者会立马接收到生产者的消息

[root@centos102 kafka_2.12-3.0.0]# bin/kafka-console-consumer.sh --bootstrap-server 192.168.27.101:9092 --topic first
nihao,kafka

网站公告

今日签到

点亮在社区的每一天
去签到