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
写入数据
33
我们从创建一个
ProducerRecord
对象开始,
ProducerRecord
对象需要包含目标主题和要发
送的内容。我们还可以指定键或分区。在发送
ProducerRecord
对象时,生产者要先把键和
值对象序列化成字节数组,这样它们才能够在网络上传输。
接下来,数据被传给分区器。如果之前在
ProducerRecord
对象里指定了分区,那么分区器
就不会再做任何事情,直接把指定的分区返回。如果没有指定分区,那么分区器会根据
ProducerRecord
对象的键来选择一个分区。选好分区以后,生产者就知道该往哪个主题和
分区发送这条记录了。紧接着,这条记录被添加到一个记录批次里,这个批次里的所有消
息会被发送到相同的主题和分区上。有一个独立的线程负责把这些记录批次发送到相应的
broker
上。
服务器在收到这些消息时会返回一个响应。如果消息成功写入
Kafka
,就返回一个
RecordMetaData
对象,它包含了主题和分区信息,以及记录在分区里的偏移量。如果写入
失败,则会返回一个错误。生产者在收到错误之后会尝试重新发送消息,几次之后如果还
是失败,就返回错误信息。
3.2
 创建
Kafka
生产者
要往
Kafka
写入消息,首先要创建一个生产者对象,并设置一些属性。
Kafka
生产者有
3
个必选的属性。
bootstrap.servers
该属性指定
broker
的地址清单,地址的格式为 host:port。清单里不需要包含所有的
broker
地址,生产者会从给定的
broker ...
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