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

79.ScottPlot的MVVM实现 C#例子 WPF例子

在你的 WPF 窗口的 XAML 文件中,定义一个,并将其Content属性绑定到 ViewModel 中的属性。这样可以将WpfPlot控件动态绑定到 ViewModel。

S13461120713的博客 2570

Kafka宝典

Kafka宝典

weixin_43955020的博客 1781

光电容积脉搏波PPG信号分析笔记

脉搏波信号PPG分析笔记

teresa_zp的专栏 6475

自绘CListCtrl时发现ON_WM_MEASUREITEM_REFLECT和DrawItem(LPDRAWITEMSTRUCT lpMeasureItemStruct) 无响应

自绘CListCtrl时发现ON_WM_MEASUREITEM_REFLECT和DrawItem(LPDRAWITEMSTRUCT lpMeasureItemStruct) 无响应 一般情况下,我们都是在 dialog 里面 放一个 list 控件,然后 用自己的类绑定自己的自绘对象,如同下面: 1.  在 dialog 里面 加一个 list 列表 2. 然后 绑定这个列表 哈哈...

mfreesky的专栏 2169

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

避坑指南:STM32F4的ADC采样FSR传感器,如何稳定读数并校准压力值?

本文详细解析了STM32F4的ADC采样FSR薄膜压力传感器时遇到的读数不稳定和非线性校准问题,提供了从硬件配置到软件滤波的完整解决方案。通过优化ADC设置、改进滤波算法和实施分段线性校准,帮助工程师实现高精度压力测量,特别适用于智能手套和医疗监测等应用场景。

weixin_28675839的博客 273

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

STM32H743 SD卡读写避坑实录:CubeMX配置MDMA给FATFS,我的FreeRTOS项目踩了哪些雷?

本文详细解析了STM32H743在CubeMX配置中结合SDMMC、MDMA与FATFS的实战避坑指南,特别针对FreeRTOS环境下的SD卡读写问题提供了解决方案。从时钟配置到FATFS参数设置,再到DMA缓存维护和FreeRTOS集成技巧,帮助开发者高效解决数据损坏、DMA传输错误等常见问题,提升项目稳定性。

weixin_30878501的博客 493

windows exe爬虫:exe抓包

在不知情所拦截应用使用的流量时,所以建议用Wireshark。本文利用python代码,实现自动拦截,写入pcap包,并用代码分析流量的协议。这样会在本地目录下生成capture_inbound.pcap,可以用wireshark分析,也可以利用python代码进行分析,如果使用python代码进行分析,需要借助pyshark或者scapy。使用的python WinDivert的包,当然前提也是需要安装Wireshark软件,设置环境变量,并安装pydirvert包。

youbingchen的博客 616

Kafka

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

qq_39128254的博客 699

3. NVIDIA GPU Xid 常见错误码全解析、故障定位与标准化处理方案

摘要: Xid是NVIDIA显卡驱动记录的硬件/软件异常日志,通过dmesg等工具可获取,包含PCI总线、GPU编号、故障码等信息,是诊断显卡问题的核心依据。Xid故障分为四类:应用程序类(如代码错误,Xid13/31)、显存ECC硬件类(如显存损坏,Xid48/63)、链路/供电温控类(如PCIe故障,Xid74/79)和GSP固件类(如静电干扰,Xid119/154)。排查流程包括日志采集、软件修复(如重启进程、升级驱动)、硬件检查(如PCIe链路状态)及断电复位(针对GSP故障)。预防措施包括监控EC

jundao1997的专栏 660

Kafka02 - Kafka快速入门

Kafka02 - Kafka快速入门

qq_43350524的博客 1027

自定义View系列教程04--Draw源码分析及其实践

通过之前的详细分析,我们知道:在measure中测量了View的大小,在layout阶段确定了View的位置。 完成这两步之后就进入到了我们相对熟悉的draw阶段,在该阶段真正地开始对视图进行绘制。按照之前的惯例,我们来瞅瞅View中draw( )的源码 public void draw(Canvas canvas) { final int privateFlags = mPriv

谷哥的小弟 2万+

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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

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

墨家巨子@俏如来 6471

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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万+

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

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、付费专栏及课程。

余额充值