การผสานรวม Schema Registry กับ Kafka
นำ Schema Registry มาใช้ในแอปพลิเคชัน Kafka เพื่อจัดการและบังคับใช้สคีมาข้อมูลโดยอัตโนมัติ
การผสานรวม Schema Registry กับ Kafka เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Apache Kafka & Stream Processing Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Integrate Schema Registry?
You've learned about Kafka and Schema Registry. Now, let's connect them! Integrating Schema Registry into your Kafka applications is vital for ensuring data quality and compatibility.
It acts as a central repository for schemas, allowing producers and consumers to validate and evolve data formats safely.
How it Works: Serializers
To integrate, Kafka clients use special serializers and deserializers that communicate with the Schema Registry.
When a producer sends data, the KafkaAvroSerializer (or Protobuf/JSON Schema equivalent) takes your data, registers its schema (if new), and then prefixes the data with a schema ID before sending it to Kafka.
Producer Configuration Essentials
To make your Kafka producer work with Schema Registry, you need to set specific properties. These tell the producer where the Schema Registry is and which serializer to use.
key.serializer: OftenStringSerializerorKafkaAvroSerializer.value.serializer: Set this toio.confluent.kafka.serializers.KafkaAvroSerializer.schema.registry.url: The URL of your Schema Registry instance (e.g.,http://localhost:8081).
Producer Code: Defining an Avro Schema
Before we send data, we need to define its structure using an Avro schema. For simplicity, we'll create a basic 'User' schema with a name and age field.
This schema will be used to create a GenericRecord.
import org.apache.avro.Schema;
public class AvroSchemaDef {
public static final String USER_SCHEMA_JSON =
"{\"namespace\": \"com.coddykit\", " +
"\"type\": \"record\", " +
"\"name\": \"User\", " +
"\"fields\": [" +
"{\"name\": \"name\", \"type\": \"string\"}," +
"{\"name\": \"age\", \"type\": \"int\"}]}";
public static final Schema USER_SCHEMA =
new Schema.Parser().parse(USER_SCHEMA_JSON);
public static void main(String[] args) {
System.out.println("User Schema Defined!");
}
}Producer Code: Sending Avro Data
Here's a complete Java producer application. Notice how we configure the serializers and the Schema Registry URL. We then create a GenericRecord based on our USER_SCHEMA and send it.
Try running this example!
import org.apache.kafka.clients.producer.*;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import java.util.Properties;
public class AvroProducer {
public static final String USER_SCHEMA_JSON =
"{\"namespace\": \"com.coddykit\", " +
"\"type\": \"record\", " +
"\"name\": \"User\", " +
"\"fields\": [" +
"{\"name\": \"name\", \"type\": \"string\"}," +
"{\"name\": \"age\", \"type\": \"int\"}]}";
public static final Schema USER_SCHEMA =
new Schema.Parser().parse(USER_SCHEMA_JSON);
public static void main(String[] args) {
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
props.put("schema.registry.url", "http://localhost:8081");
Producer<String, GenericRecord> producer = new KafkaProducer<>(props);
String topic = "avro-users";
GenericRecord user = new GenericData.Record(USER_SCHEMA);
user.put("name", "Coddy");
user.put("age", 5);
ProducerRecord<String, GenericRecord> record = new ProducerRecord<>(topic, "user-1", user);
try {
producer.send(record, (metadata, exception) -> {
if (exception == null) {
System.out.println("Sent record to topic " + metadata.topic() + " partition " + metadata.partition() + " offset " + metadata.offset());
} else {
exception.printStackTrace();
}
});
} finally {
producer.flush();
producer.close();
}
}
}How it Works: Deserializers
On the consumer side, the KafkaAvroDeserializer (or equivalent) plays the opposite role.
When a consumer receives a message, the deserializer extracts the schema ID, fetches the corresponding schema from Schema Registry, and then uses that schema to correctly deserialize the message back into your application's data type (e.g., a GenericRecord or a specific Avro object).
Consumer Configuration Essentials
Similar to producers, Kafka consumers also need specific properties to work with Schema Registry:
key.deserializer: OftenStringDeserializerorKafkaAvroDeserializer.value.deserializer: Set this toio.confluent.kafka.serializers.KafkaAvroDeserializer.schema.registry.url: The URL of your Schema Registry instance.group.id: A unique ID for your consumer group.auto.offset.reset: Defines behavior when no initial offset is found (e.g.,earliestorlatest).
Consumer Code: Receiving Avro Data
This consumer application is configured to read Avro messages from the 'avro-users' topic. It uses KafkaAvroDeserializer to automatically handle schema resolution.
Run this example AFTER running the producer to see the data!
import org.apache.kafka.clients.consumer.*;
import io.confluent.kafka.serializers.KafkaAvroDeserializer;
import org.apache.avro.generic.GenericRecord;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
public class AvroConsumer {
public static void main(String[] args) {
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "avro-consumer-group");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class.getName());
props.put("schema.registry.url", "http://localhost:8081");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
Consumer<String, GenericRecord> consumer = new KafkaConsumer<>(props);
String topic = "avro-users";
consumer.subscribe(Collections.singletonList(topic));
System.out.println("Listening for messages on topic: " + topic);
try {
while (true) {
ConsumerRecords<String, GenericRecord> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, GenericRecord> record : records) {
System.out.printf("Received record (key=%s, value=%s, partition=%d, offset=%d)\n",
record.key(), record.value(), record.partition(), record.offset());
GenericRecord user = record.value();
System.out.println(" User Name: " + user.get("name") + ", Age: " + user.get("age"));
}
}
} finally {
consumer.close();
}
}
}Benefits of Seamless Integration
Integrating Schema Registry with your Kafka clients offers significant advantages:
- Data Compatibility: Ensures producers and consumers always understand each other's data formats.
- Schema Evolution: Safely update schemas over time without breaking existing applications.
- Data Governance: Centralized schema management provides a single source of truth for your data structures.
- Reduced Boilerplate: Serializers/deserializers handle schema management automatically.
Quick Check: Schema Registry Setup
Which of the following properties are essential for a Kafka client (producer or consumer) to integrate with Confluent Schema Registry using Avro?
Recap: Integrating Schema Registry
In this lesson, you learned how to integrate Confluent Schema Registry with your Kafka applications.
- We configured Kafka producers and consumers with `schema.registry.url`.
- We used `KafkaAvroSerializer` and `KafkaAvroDeserializer` to handle Avro data automatically.
- You saw practical examples of sending and receiving `GenericRecord`s.
This integration is key for robust, schema-driven data pipelines!
เรียนรู้ Apache Kafka & Stream Processing Fundamentals ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การผสานรวม Schema Registry กับ Kafka” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสานรวม Schema Registry กับ Kafka” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Apache Kafka & Stream Processing Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวม Schema Registry กับ Kafka”
นำ Schema Registry มาใช้ในแอปพลิเคชัน Kafka เพื่อจัดการและบังคับใช้สคีมาข้อมูลโดยอัตโนมัติ คุณปฏิบัติ Apache Kafka & Stream Processing Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Apache Kafka & Stream Processing Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Apache Kafka & Stream Processing Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสานรวม Schema Registry กับ Kafka” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม
ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องจัดการสคีมา
- สคีมา Avro และ Protobuf
- การผสานรวม Schema Registry กับ Kafka
- วิวัฒนาการของสคีมาและโหมดความเข้ากันได้