Skip to Content
Kafka权威指南
book

Kafka权威指南

by Neha Narkhede, Gwen Shapira, Todd Palino
January 2018
Beginner to intermediate
234 pages
7h 8m
Chinese
Posts & Telecom Press
Content preview from Kafka权威指南
4
Kafka
消费者——从
Kafka
读取数据
应用程序使用 KafkaConsumer
Kafka
订阅主题,并从订阅的主题上接收消息。从
Kafka
读取数据不同于从其他消息系统读取数据,它涉及一些独特的概念和想法。如果不先理解
这些概念,就难以理解如何使用消费者
API
。所以我们接下来先解释这些重要的概念,然
后再举几个例子,演示如何使用消费者
API
实现不同的应用程序。
4.1
 
KafkaConsumer
概念
要想知道如何从
Kafka
读取消息,需要先了解消费者和消费者群组的概念。以下章节将解
释这些概念。
4.1.1
 消费者和消费者群组
假设我们有一个应用程序需要从一个
Kafka
主题读取消息并验证这些消息,然后再把它们
保存起来。应用程序需要创建一个消费者对象,订阅主题并开始接收消息,然后验证消息
并保存结果。过了一阵子,生产者往主题写入消息的速度超过了应用程序验证数据的速
度,这个时候该怎么办?如果只使用单个消费者处理消息,应用程序会远跟不上消息生成
的速度。显然,此时很有必要对消费者进行横向伸缩。就像多个生产者可以向相同的主题
写入消息一样,我们也可以使用多个消费者从同一个主题读取消息,对消息进行分流。
Kafka
消费者从属于
消费者群组
。一个群组里的消费者订阅的是同一个主题,每个消费者
接收主题一部分分区的消息。
假设主题
T1
4
个分区,我们创建了消费者
C1
,它是群组
G1
里唯一的消费者,我们用
它订阅主题
T1
。消费者
C1
将收到主题
T1
全部
4
个分区的消息,如图
4-1
所示。
48
Kafka
消费者——从
Kafk ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習

精通機器學習

Aurélien Géron
精通特征工程

精通特征工程

Alice Zheng, Amanda Casari
高效能PYTHON程式設計

高效能PYTHON程式設計

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9787115473271