kafka第四篇--快速入门(如何使用kafka)

《Kafka系列》Kafka详细教程入门 kafka是分布式的基于消息的发布-订阅的消息队列。LinkedIn(领英),Scala编写的​ kafka消费者在消费数据的时候,一般都是分组的。这个分组叫做消费者组。不同组的消费相互之间不影响。相同组内的消费,同组内的偏移量相互影响;但是需要注意的是,如果你的分区是3个,那么你的消费者组内的消费者最优是3个,因为再多也没有用了,因为组内的消费者,一个消费者消费一个主题的分区。 阅读详情

Quick Start

Step 1: Download the code

Download  the 0.8 release.
> tar xzf kafka-<VERSION>.tgz
> cd kafka-<VERSION>
> ./sbt update
> ./sbt package
> ./sbt assembly-package-dependency
This tutorial assumes you are starting on a fresh zookeeper instance with no pre-existing data. If you want to migrate from an existing 0.7 installation you will need to follow the migration instructions.

Step 2: Start the server

Kafka uses zookeeper so you need to first start a zookeeper server if you don't already have one. You can use the convenience script packaged with kafka to get a quick-and-dirty single-node zookeeper instance.

> bin/zookeeper-server-start.sh config/zookeeper.properties
[2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...
Now start the Kafka server:
> bin/kafka-server-start.sh config/server.properties
[2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties)
[2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties)
...

Step 3: Create a topic

Let's create a topic named "test" with a single partition and only one replica:
> bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 1 --partition 1 --topic test
We can now see that topic if we run the list topic command:
> bin/kafka-list-topic.sh --zookeeper localhost:2181
Alternatively, you can also configure your brokers to auto-create topics when a non-existent topic is published to.

Step 4: Send some messages

Kafka comes with a command line client that will take input from a file or standard in and send it out as messages to the Kafka cluster. By default each line will be sent as a separate message.

Run the producer and then type a few messages to send to the server.

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test 
This is a message
This is another message

Step 5: Start a consumer

Kafka also has a command line consumer that will dump out messages to standard out.
> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
This is a message
This is another message

If you have each of the above commands running in a different terminal then you should now be able to type messages into the producer terminal and see them appear in the consumer terminal.

All the command line tools have additional options; running the command with no arguments will display usage information documenting them in more detail.

Step 6: Setting up a multi-broker cluster

So far we have been running against a single broker, but that's no fun. For Kafka, a single broker is just a cluster of size one, so nothing much changes other than starting a few more broker instances. But just to get feel for it, let's expand our cluster to three nodes (still all on our local machine).

First we make a config file for each of the brokers:

> cp config/server.properties config/server-1.properties 
> cp config/server.properties config/server-2.properties
Now edit these new files and set the following properties:
 
config/server-1.properties:
    broker.id=1
    port=9093
    log.dir=/tmp/kafka-logs-1
 
config/server-2.properties:
    broker.id=2
    port=9094
    log.dir=/tmp/kafka-logs-2
The  broker.id  property is the unique and permanent name of each node in the cluster. We have to override the port and log directory only because we are running these all on the same machine and we want to keep the brokers from trying to all register on the same port or overwrite each others data.

We already have Zookeeper and our single node started, so we just need to start the two new nodes. However, this time we have to override the JMX port used by java too to avoid clashes with the running node:

> JMX_PORT=9997 bin/kafka-server-start.sh config/server-1.properties &
...
> JMX_PORT=9998 bin/kafka-server-start.sh config/server-2.properties &
...
Now create a new topic with a replication factor of three:
> bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 3 --partition 1 --topic my-replicated-topic
Okay but now that we have a cluster how can we know which broker is doing what? To see that run the "list topics" command:
> bin/kafka-list-topic.sh --zookeeper localhost:2181
topic: my-replicated-topic  partition: 0  leader: 1  replicas: 1,2,0  isr: 1,2,0
topic: test	                partition: 0  leader: 0  replicas: 0      isr: 0
Here is an explanation of output:
  • "leader" is the node responsible for all reads and writes for the given partition. Each node would be the leader for a randomly selected portion of the partitions.
  • "replicas" is the list of nodes that are supposed to server the log for this partition regardless of whether they are currently alive.
  • "isr" is the set of "in-sync" replicas. This is the subset of the replicas list that is currently alive and caught-up to the leader.
Note that both topics we created have only a single partition (partition 0). The original topic has no replicas and so it is only present on the leader (node 0), the replicated topic is present on all three nodes with node 1 currently acting as leader and all replicas in sync.

As before let's publish a few messages message:

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
...
my test message 1
my test message 2
^C 
Now consume this message:
> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C
Now let's test out fault-tolerance. Kill the broker acting as leader for this topic's only partition:
> pkill -9 -f server-1.properties
Leadership should switch to one of the slaves:
> bin/kafka-list-topic.sh --zookeeper localhost:2181
...
topic: my-replicated-topic	partition: 0	leader: 2	replicas: 1,2,0	isr: 2
topic: test	partition: 0	leader: 0	replicas: 0	isr: 0
And the messages should still be available for consumption even though the leader that took the writes originally is down:
> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

转载自:http://kafka.apache.org/08/quickstart.html

【翻译】Kafka快速入门 Kafka 可以使用 KRaft 模式运行,使用本地脚本和下载的文件或 Docker 镜像。按照下面的一个部分来启动 Kafka 服务器,但不要同时执行两个部分。默认情况下,您输入的每一行都将导致将一个单独的事件写入主题。一旦所有的服务都成功启动,你就有了一个基本的 Kafka 环境,可以运行并准备使用了。一旦 Kafka 服务器成功启动,你将有一个基本的 Kafka 环境运行并准备使用。尝试更多操作:切换回生产者终端写入额外的事件,并观察消费者终端中的实时变化。启动 Kafka Docker 容器。 阅读详情

相关推荐

Kafka快速入门

消息队列(message queue)简称MQ,是一种以“先进先出”的数据结构为基础的消息服务器。消息:两个系统间要传输的数据作用:实现消息的传递原始的数据传递方式:传输效率较低。基于MQ实现消息的传输,如下图所示:上述的数据的传输方式属于异步传输【作为调用方法不用等待被调用方执行完毕就可以接续传递消息】,数据传输的效率较高。services:zookeeper:ports:volumes:services:定义 Docker Compose 文件中的服务。zookeeper。

m0_61925586的博客 9000

python中kafka的安装和使用教程

Kafka是一个分布式流处理系统,流处理系统使它可以像消息队列一样publish(发布)或者subscribe(订阅)消息,分布式提供了容错性,并发处理消息的机制。kafka运行在集群上,集群包含一个或多个服务器。kafka把消息存在topic中,每一条消息包含键值(key),值(value)和时间戳(timestamp)。

菜鸡小白的成长记录 8142

别再手动算临界转速了!用ANSYS Workbench 2023 R2快速搞定转子坎贝尔图(附完整APDL命令流)

本文详细介绍了如何利用ANSYS Workbench 2023 R2快速生成转子坎贝尔图,实现全自动临界转速分析。通过对比传统手动计算与Workbench自动化流程的效率差异,展示了其在旋转机械设计中的革命性优势,包括大幅缩短分析周期、提升精度至98.7%。文章还提供了完整的APDL命令流和实战案例,帮助工程师快速掌握这一高效工具。

weixin_42699679的博客 470

Kafka宝典

Kafka宝典

weixin_43955020的博客 1781

Linux下基于AHCI controller模块实现SATA Platform驱动附代码详细流程

本文主要是针对ARM A55集成了AHSATA controller模块开发sata驱动,通过Platform的形式在linux识别到SSD,本文详细讲述整个开发过程,同时对SATA Platform驱动进行讲解。1、配置SATA Phy的初始化;2、打开SATA Platform相关编译开关;3、配置设备树;4、linux启动识盘。

Luckiers的博客 2497

Apollo control模块横向控制原理及核心代码逐行解析

看完这篇文章,你会对apollo横向控制有一个整体的概念

weixin_39199083的博客 2万+

node与kafka结合

centos7安装kafka 参考文章:https://www.cnblogs.com/yangk1996/p/10841588.html 安装注意事项(以阿里云服务器为例) 在配置config下面的server.properties文件,增加ip映射,如果不增加就是locahost。这样没办法使用java连接host.name=192.168.100.129,这句话去掉,改成如下内容: a...

qq_33332184的博客 1484

【C++】POCO学习总结(六):线程、线程池、同步

Poco::RunnableAdapter继承自Poco::Runnable,在Poco::RunnableAdapter构造函数中指定了类和方法;Poco::ThreadLocal是线程局部变量,也叫线程局部存储,意思是模版ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的变量。

郭老二 5098

nodejs--kafka

producer.js var kafka = require('kafka-node'); var Producer = kafka.Producer; // 建立与kafka的连接 var client = new kafka.KafkaClient('localhost:9092'); // 创建生产者 var producer = new Producer(client); // topic var topic = 'test'; var payloads = [{ // 需要发送的一些配置

weixin_42282999的博客 1568

数字IC后端笔记-libcell命名规则与标准单元库介绍

ND3D1BWP13T40P140HVTND3D1BWP13T40P140HVTTSMC两种工艺,Metalization Stack的截面示意图:还有一层AP用来走RDL,RDL这层是最厚的一层,一般用于连接IO的bump,信号线基本上不会用这层绕线。

sinat_41774721的博客 2463

2024-12月js逆向案例-sensor-data之vmp字段之akamai_2/3.0-(下)

akamai2/3的vmp字段逆向分析

十一姐的博客 3144

Kafka

Flume缺点: 1.数据都保存在内存中,数据容易丢失. 2.增加消费者不容易. 3.数据无法长时间保留. eg:微信的聊天记录保存功能 保存策略 缺点 数据无法长期保存,客户如果想延迟获取做不到 增加消费者,OCP(开发原则)[Open Closed Principle 开闭原则,是Java世界里最基础的设计原则,简单来说就是对添加开放,对修改关闭] 重复消费 -----------内...

qq_39128254的博客 699

《PLECS仿真与实战:从建模到高端应用》-专栏文件目录

《PLECS仿真与实战:从建模到高端应用》是一本系统介绍PLECS电力电子仿真软件的实用指南。全书共10章,首先介绍软件安装与基础操作,包括RC电路仿真等入门内容;随后深入讲解各类电力电子电路的建模与仿真,涵盖整流、交交变换、直流变换、逆变等电路;最后探讨PWM控制建模和直流调速系统等高端应用。书中配有丰富的示例程序和波形分析方法,附录还包含电力电子器件、代码生成等实用资料。该书适合电力电子领域工程师和学生系统学习电路仿真技术。

540

Kafka02 - Kafka快速入门

Kafka02 - Kafka快速入门

qq_43350524的博客 1027

AgentScope 2.0 API完全指南:构建企业级AI智能体应用的技术实践

本文系统梳理AgentScope 2.0框架的完整API体系,涵盖智能体核心引擎、统一模型适配层、工具系统、消息协议、权限控制与工作空间管理等七大核心模块。通过深入的源码级分析与架构图示,详细解读Agent类的推理-行动循环、ChatModelBase的多模型抽象、Toolkit的工具管理机制、ContentBlock的多模态内容表达、PermissionEngine的安全决策流程,以及Local/Docker/E2B三种工作空间实现。文章还提供了智能客服系统的完整实战案例,展示从自定义工具开发到流式服务部

program哲学 407

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

一.Kafka入门到精通-Kafka快速入门

前言 在内卷严重的程序员圈子中,原地踏步就是退步,所以不能再躺平啦,赶紧爬起来学习,接下来博主将推出《Kafka入门到精通》系列文章,让你可以在企业中玩起Kafka来得心应手,此乃升职加薪必备呀。 Kafka认识 Kafka 是由Linkedin公司开发的,支持多分区、多副本,基于 Zookeeper 的分布式消息流平台,是一款基于发布订阅模式的开源消息引擎系统。相对于其他的消息组件来说Kafka拥有更好的吞吐量、内置分区、具有复制和容错的功能,这使它成为一个非常理想的大型消息处理应用。使用场景如:网页浏览

墨家巨子@俏如来 6471

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Kafka入门使用

Kafka是一个分布式消息队列,因其可水平扩展和高吞吐率而被广泛使用! kafka的安装与使用(Windows) 1、运行zookeeper,下载与安装可见:https://blog.youkuaiyun.com/z1790424577/article/details/106660764 2、下载kafka,地址为:https://kafka.apache.org/downloads 下载完成并解压缩后,打开控制台进入kafka安装目录输入如下命令启动kafka: kafka-server-start.bat .\c

逝去的往事的博客 1万+

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Kafka 快速入门

Apache Kafka 是分布式的、基于 发布/订阅 的容错消息系统。主要特性如下:高吞吐、低延迟:可以做到每秒百万级的吞吐量,并且时延极低持久性、可靠性:消息被持久化到本地磁盘,支持数据备份防止数据丢失,具备以时间复杂度为 O(1) 的方式提供消息持久化能力;并且可以配置消息有效期,以便消费者可以多次消费kafka的管理UI,这里主要推荐三个:kafka-ui:官方文档地址为。相对于EFAK,功能显得极为简陋,但基本够用,最主要是他支持kraft。另外其github上的star数量也远超EFAK。

qq_62866151的博客 1352

快速入门Kafka系列(4)——Kafka的主要组件说明

        作为快速入门Kafka系列的第四篇博客,本篇为大家带来的是Kafka的主要组件说明~         码字不易,先赞后看! 文章目录Kafka主要组件说明1、kafka当中的producer说明2、kafka当中的topi...

大数据梦想家 8355

快速入门Kafka系列(2)——Kafka集群搭建

        作为快速入门Kafka系列的第二篇博客,本篇为大家带来的是Kafka集群搭建~         码字不易,先赞后看! 文章目录Kafka 集群环境搭建1、初始化环境准备2、下载安装包并上传解压3、node01服务器修改ka...

大数据梦想家 6418

快速入门Kafka系列(5)——Kafka的命令行操作

        作为快速入门Kafka系列的第五篇博客,本篇为大家带来的是Kafka的主要组件说明~         码字不易,先赞后看! 文章目录Kafka集群操作1. 查看当前服务器中所有Topic2. 创建topic3. 删除...

大数据梦想家 6332

Kafka入门

Kafka是一个分布式系统,由通过高性能TCP网络协议进行通信的服务器和客户端组成。它可以部署在本地和云环境的裸机硬件、虚拟机和容器上。 服务器:Kafka作为一个或多个服务器的集群运行,可以跨越多个数据中心或云区域。其中一些服务器形成存储层,称为代理。其他服务器运行Kafka Connect以将数据作为事件流持续导入和导出,以及将Kafka与现有的系统(例如关系数据库以及其他kafka集群)集成。为了实现关键任务用例,Kafka集群具有高度可扩展性和容错性:如果其中任何一个服务器出现故障,其他服务器将

qq_45039852的博客 1万+

Kafka快速入门及使用

Kafka快速学习入门,并在Spring中配置使用Kafka

花木凋零成兰的博客 1493
上一篇: kafka第三篇--安装使用
下一篇: kafka第五篇
crazyhacking
博客等级 码龄15年 299粉丝 148原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值