Avro สำหรับการกำหนดสคีมา
เรียนรู้การกำหนดสคีมาเหตุการณ์ด้วย Apache Avro ซึ่งเป็นเฟรมเวิร์กการจัดลำดับข้อมูลที่กะทัดรัดและมีประสิทธิภาพ
Avro สำหรับการกำหนดสคีมา เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Apache Avro?
Welcome! In this lesson, we'll explore Apache Avro, a powerful data serialization system. It's widely used in event-driven architectures, especially with Kafka.
Avro helps define the structure of your data (events) using a schema. This schema acts like a blueprint, ensuring consistency and enabling seamless communication between different applications.
Why Use Avro Schemas?
Avro's schema-driven approach offers several benefits:
- Data Compactness: Avro serializes data efficiently, leading to smaller message sizes.
- Schema Evolution: It provides robust rules for how schemas can change over time without breaking old applications.
- Language Agnostic: Schemas are language-independent, allowing different programming languages to read and write the same data.
- Strong Typing: Ensures data types are consistent, reducing runtime errors.
Avro Schema Structure
Avro schemas are defined using JSON. Each schema describes a data structure, most commonly a record type.
Key elements of an Avro schema include:
type: The kind of schema (e.g., "record", "enum", "array").name: The name of the schema (e.g., "User", "Order").namespace: An optional string to qualify the name.fields: For record types, an array of field definitions.
Avro Primitive Types
Avro supports a set of basic, primitive data types:
null: No value.boolean:trueorfalse.int: 32-bit signed integer.long: 64-bit signed integer.float: Single precision (32-bit) floating-point number.double: Double precision (64-bit) floating-point number.bytes: Sequence of 8-bit unsigned bytes.string: Unicode character sequence.
Defining a Simple Record
Let's define a simple Avro record schema for a User. Each field needs a name and a type.
Here, id is an int and name is a string.
{
"type": "record",
"name": "User",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "id", "type": "int"},
{"name": "name", "type": "string"}
]
}Complex Type: Nested Records
You can define complex data structures by nesting records. For example, an Order record might contain a Customer record within one of its fields.
{
"type": "record",
"name": "Order",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "orderId", "type": "string"},
{"name": "customer", "type": {
"type": "record",
"name": "Customer",
"fields": [
{"name": "customerId", "type": "int"},
{"name": "email", "type": "string"}
]
}}
]
}Complex Types: Arrays and Maps
Avro also supports collection types:
array: For a list of items of the same type. Theitemsproperty specifies the type of elements.map: For key-value pairs, where keys are alwaysstrings. Thevaluesproperty specifies the type of values.
{
"type": "record",
"name": "Product",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "productId", "type": "string"},
{"name": "tags", "type": {"type": "array", "items": "string"}},
{"name": "attributes", "type": {"type": "map", "values": "string"}}
]
}Complex Type: Enums
An enum defines a fixed set of named values. This is useful for fields with a limited, predefined set of options.
The symbols property lists all allowed string values for the enum.
{
"type": "record",
"name": "Payment",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "transactionId", "type": "string"},
{"name": "status", "type": {
"type": "enum",
"name": "PaymentStatus",
"symbols": ["PENDING", "COMPLETED", "FAILED"]
}}
]
}Complex Type: Unions & Nullables
A union allows a field to be one of several specified types. This is crucial for defining optional or nullable fields.
To make a field nullable, you define its type as a union of "null" and its actual type, e.g., ["null", "string"]. The first type in the union is often the default if not explicitly set.
{
"type": "record",
"name": "LogEntry",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "timestamp", "type": "long"},
{"name": "message", "type": "string"},
{"name": "userId", "type": ["null", "string"], "default": null}
]
}Setting Default Field Values
You can specify a default value for a field. This is particularly important for schema evolution, as it ensures that older data (without the new field) can still be read without error, using the default value.
The default value must be a valid instance of the field's type.
{
"type": "record",
"name": "Settings",
"namespace": "com.coddykit.avro",
"fields": [
{"name": "theme", "type": "string", "default": "dark"},
{"name": "notificationsEnabled", "type": "boolean", "default": true}
]
}Avro Schema Question
Consider an Avro record for an event. We need a field named comment that can contain a string but is optional (can be null) and defaults to null if not provided.
Avro Definition Recap
Great job! You've learned the fundamentals of defining schemas with Apache Avro.
- Avro schemas are JSON-based blueprints for your data.
- They support primitive types (int, string, etc.) and complex types (records, arrays, maps, enums, unions).
- Unions are key for defining nullable or optional fields.
- Default values are crucial for backward compatibility and schema evolution.
Next, we'll see how to integrate these schemas with Spring Boot and Schema Registry.
เรียนรู้ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “Avro สำหรับการกำหนดสคีมา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Avro สำหรับการกำหนดสคีมา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Avro สำหรับการกำหนดสคีมา”
เรียนรู้การกำหนดสคีมาเหตุการณ์ด้วย Apache Avro ซึ่งเป็นเฟรมเวิร์กการจัดลำดับข้อมูลที่กะทัดรัดและมีประสิทธิภาพ คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “Avro สำหรับการกำหนดสคีมา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม
ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความสำคัญของการจัดการสคีมา
- Avro สำหรับการกำหนดสคีมา
- การผสาน Spring Boot กับ Schema Registry
- วิวัฒนาการของสคีมาและโหมดความเข้ากันได้