当前位置: 首页 > 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

相关文章:

  • P1064 [NOIP 2006 提高组] 金明的预算方案——依赖背包
  • k8s热更新-subPath 不支持热更新
  • 界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
  • 数据结构(7)—— 二叉树(1)
  • 微信小程序动态组件加载的应用场景与实现方式
  • 字节开源FlowGram:AI时代可视化工作流新利器
  • 【Axure视频教程】下载和安装Axure汉化包
  • 深度解析Mysql中MVCC的工作机制
  • 内存管理【Linux操作系统】
  • .Net Framework 4/C# 面向对象编程进阶
  • 【2025】通过idea把项目到私有仓库(3)
  • 宏基因组产品升级——微生物菌群木质素降解能力评估!
  • 中科君芯JFG150N40B 40V-N沟道增强模式功率驱动器
  • Go语言依赖管理与版本控制-《Go语言实战指南》
  • [蓝桥杯]最大比例
  • [蓝桥杯]三元组中心问题
  • 如何在mac上安装podman
  • 机器学习监督学习sklearn实战三:八种算法对印第安人糖尿病预测数据进行分类和比较
  • 在WPS中如何启用宏VBA wps.vba.exe下载和安装
  • 归一化 Normalization 技术概述、优化思路
  • 十堰微网站建设/百度指数大数据
  • 网站源码 一品资源网/网站首页排名seo搜索优化
  • 个人备案可以做哪些网站/百度竞价客服
  • 加强局网站建设/金蝶进销存免费版
  • 网站logo怎么做/百度收录技巧
  • 移动网站建设的基本流程图/推广普通话手抄报简单又好看