สคีมา Avro และ Protobuf
เรียนรู้รูปแบบการทำให้เป็นอนุกรมยอดนิยม เช่น Avro และ Protobuf รวมถึงวิธีใช้ร่วมกับ Schema Registry
สคีมา Avro และ Protobuf เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Apache Kafka & Stream Processing Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Data Formats for Kafka
When sending data through Kafka, it's just bytes. To make sense of these bytes, both the sender (producer) and receiver (consumer) need to agree on a common structure.
This is where data serialization formats and schemas come in, providing a blueprint for your data.
Meet Apache Avro
Apache Avro is a popular, language-agnostic data serialization system. It relies heavily on schemas to define the structure of data.
- Compactness: Data is serialized into a compact binary format.
- Schema Evolution: Avro handles schema changes gracefully, allowing producers and consumers with different schema versions to communicate.
- Language Agnostic: Tools exist for many programming languages.
Avro Schemas: JSON Power
Avro schemas are defined using JSON. This makes them human-readable and easy to manage. A schema describes the data's fields, their types, and any default values.
Basic Avro types include string, int, long, boolean, float, double, bytes, and null.
Building an Avro Schema
Let's define a simple Avro schema for a "User" record. Notice the type, name, and fields with their own name and type.
{
"type": "record",
"name": "User",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "name", "type": "string"},
{"name": "age", "type": ["int", "null"], "default": 0}
]
}How Avro Uses Schemas
With Avro, the schema travels with the data (or is known by the consumer via Schema Registry). This means the actual data payload is very small, as field names and types aren't repeated for every message.
The schema acts as a contract, ensuring data consistency and enabling efficient serialization and deserialization.
Meet Protocol Buffers (Protobuf)
Protocol Buffers (Protobuf) is Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. It's designed to be smaller and faster than XML.
- Efficiency: Very compact binary format.
- Code Generation: Compilers generate code for various languages based on
.protodefinitions. - Backward/Forward Compatibility: Supports schema evolution through careful field numbering.
Protobuf Schemas: .proto Files
Protobuf schemas are defined in .proto files using a special syntax. You define message types, which are like classes, and specify fields within them.
Each field requires a type, a name, and a unique field number. These numbers are crucial for backward and forward compatibility.
Building a Protobuf Schema
Here's a simple .proto definition for a "Product" message. Notice the syntax, message keyword, and the assigned field numbers (e.g., 1, 2).
syntax = "proto3";
package com.coddykit.protobuf;
message Product {
string id = 1;
string name = 2;
double price = 3;
}Avro vs. Protobuf: A Quick Comparison
Both Avro and Protobuf are excellent for data serialization, but they have different characteristics:
- Schema Format: Avro uses JSON; Protobuf uses its own
.protosyntax. - Code Generation: Avro is schema-first (can generate code); Protobuf is often code-first (generates code from
.proto). - Data Size: Both are very compact, often outperforming JSON/XML.
- Schema Evolution: Both support it, but with different strategies (Avro relies on Schema Registry, Protobuf on field numbers).
Quick Check
You've learned about Avro and Protobuf. Which statement correctly identifies the primary way Avro and Protobuf schemas are defined?
Recap: Structured Data for Kafka
Great job! In this lesson, we explored two powerful data serialization formats: Apache Avro and Protocol Buffers (Protobuf).
- Avro uses JSON for schema definitions and is excellent for schema evolution.
- Protobuf uses a
.protosyntax with field numbers for compact, efficient data.
Both help ensure data consistency and efficiency when used with Kafka and Schema Registry, which we'll explore further next!
เรียนรู้ Apache Kafka & Stream Processing Fundamentals ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “สคีมา Avro และ Protobuf” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สคีมา Avro และ Protobuf” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Apache Kafka & Stream Processing Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สคีมา Avro และ Protobuf”
เรียนรู้รูปแบบการทำให้เป็นอนุกรมยอดนิยม เช่น Avro และ Protobuf รวมถึงวิธีใช้ร่วมกับ Schema Registry คุณปฏิบัติ Apache Kafka & Stream Processing Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Apache Kafka & Stream Processing Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Apache Kafka & Stream Processing Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “สคีมา Avro และ Protobuf” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม
ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องจัดการสคีมา
- สคีมา Avro และ Protobuf
- การผสานรวม Schema Registry กับ Kafka
- วิวัฒนาการของสคีมาและโหมดความเข้ากันได้