当前位置: 首页 > news >正文

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

基础命令

主题

参数含义
–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=1073741824Topic: 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=1073741824Topic: first	Partition: 0	Leader: 2	Replicas: 2,1,0	Isr: 2,1,0Topic: first	Partition: 1	Leader: 0	Replicas: 0,1,2	Isr: 0,1,2Topic: 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
http://www.lqws.cn/news/120709.html

相关文章:

  • springboot 微服务 根据tomcat maxthread 和 等待用户数量,达到阈值后,通知用户前面还有多少用户等待,请稍后重试
  • 数学复习笔记 25
  • CMake在VS中使用远程调试
  • OpenCV C/C++ 视频播放器 (支持调速和进度控制)
  • CentOS在vmware局域网内搭建DHCP服务器【踩坑记录】
  • 浅析EXCEL自动连接PowerBI的模板
  • 如何配置一个sql server使得其它用户可以通过excel odbc获取数据
  • Vue前端篇——Vue 3的watch深度解析
  • 【设计模式-4.8】行为型——中介者模式
  • 设计模式-外观模式
  • P3156 【深基15.例1】询问学号
  • 2025年- H68-Lc176--46.全排列(回溯,组合)--Java版
  • 设计模式杂谈-模板设计模式
  • 一文了解 GPU 服务器及其在数据中心中的角色
  • 在本地查看服务器上的TensorBoard
  • 联通专线赋能,亿林网络裸金属服务器:中小企业 IT 架构升级优选方案
  • Promtail采集服务器本地日志存储到Loki
  • LeetCode 3370.仅含置位位的最小整数
  • 使用Virtual Serial Port Driver+com2tcp(tcp2com)进行两台电脑的串口通讯
  • AlphaFold3服务器安装与使用(非docker)(1)
  • 模拟搭建私网访问外网、外网访问服务器服务的实践操作
  • Orthanc:轻量级PACS服务器与DICOMweb支持的技术详解
  • Spring Framework 中 UriComponentsBuilder工具类
  • Educational Codeforces Round 179 (Rated for Div. 2)(A-E)
  • Python训练营打卡DAY44
  • DuckDB + Spring Boot + MyBatis 构建高性能本地数据分析引擎
  • 掌握 MotionLayout:交互动画开发
  • UniApp 全生命周期钩子详解
  • python版若依框架开发:python版若依部署
  • 【后端开发】goland分布式锁的几种实现方式(mysql,redis,etcd,zookeeper,mq,s3)