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

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.lryc.cn/news/2399587.html

相关文章:

  • 目标检测我来惹1 R-CNN
  • lua的笔记记录
  • 智能进化论:AI必须跨越的四大认知鸿沟
  • L2-056 被n整除的n位数 - java
  • 传统足浴行业数字化转型:线上预约平台的技术架构与商业逻辑
  • Java-IO流之字节输入流详解
  • 从OCR到Document Parsing,AI时代的非结构化数据处理发生了什么改变?
  • 【C/C++】入门grpc的idl
  • 【Java实用工具类】手撸SqlBuilder工具类,优雅拼接动态SQL,MyBatisPlus同款风格!
  • 宇树科技更名“股份有限公司”深度解析:机器人企业IPO前奏与资本化路径
  • Inno Setup 安装向导各个页面详解
  • 转战web3远程工作的英语学习的路线规划
  • OPENCV重点结构体Mat的讲解
  • Java 创建线程池的几种方式
  • 【趣味Html】第11课:动态闪烁发光粒子五角星
  • AnyIO Event:异步编程中的同步利器
  • CFTel:一种基于云雾自动化的鲁棒且可扩展的远程机器人架构
  • Educational Codeforces Round 179 (Rated for Div. 2)
  • 完成一个可交互的k8s管理平台的页面开发
  • 多线程编程技术解析及示例:pthread_cond_timedwait、pthread_mutex_lock 和 pthread_mutex_trylock
  • vue实现点击单选或者多选模式
  • Windows系统工具:WinToolsPlus 之 SQL Server 日志清理
  • 在Windows11上安装 Ubuntu WSL
  • 嵌入式Linux之RK3568
  • Elasticsearch的插件(Plugin)系统介绍
  • 提取 PDF 文件中的文字以及图片中的文字
  • JavaScript性能优化实战技术
  • LeetCode 热题 100 739. 每日温度
  • 网页前端开发(基础进阶3--Vue)
  • tryhackme——Abusing Windows Internals(进程注入)