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权威指南
Kafka
消费者——从
Kafka
读取数据
59
在使用自动提交时,每次调用轮询方法都会把上一次调用返回的偏移量提交上去,它并不
知道具体哪些消息已经被处理了,所以在再次调用之前最好确保所有当前调用返回的消息
都已经处理完毕(在调用
close() 方法之前也会进行自动提交)。一般情况下不会有什么问
题,不过在处理异常或提前退出轮询时要格外小心。
自动提交虽然方便,不过并没有为开发者留有余地来避免重复处理消息。
4.6.2
 提交当前偏移量
大部分开发者通过控制偏移量提交时间来消除丢失消息的可能性,并在发生再均衡时减少
重复消息的数量。消费者
API
提供了另一种提交偏移量的方式,开发者可以在必要的时候
提交当前偏移量,而不是基于时间间隔。
enable.auto.commit 设为 false,让应用程序决定何时提交偏移量。使用 commitSync()
提交偏移量最简单也最可靠。这个
API
会提交由 poll() 方法返回的最新偏移量,提交成
功后马上返回,如果提交失败就抛出异常。
要记住,commitSync() 将会提交由 poll() 返回的最新偏移量,所以在处理完所有记录后要
确保调用了 commitSync(),否则还是会有丢失消息的风险。如果发生了再均衡,从最近一
批消息到发生再均衡之间的所有消息都将被重复处理。
下面是我们在处理完最近一批消息后使用 commitSync() 方法提交偏移量的例子。
while (true) {
ConsumerRecords<String, String> records ...
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