0Pricing
Apache Kafka & Stream Processing Fundamentals · บทเรียน

Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams

เรียนรู้ว่า Kafka Streams ใช้ Serdes เพื่อทำให้คีย์และค่าเป็นอนุกรมและย้อนกลับจากอนุกรมอย่างไร รวมถึงวิธีกำหนดค่า Serdes เริ่มต้นและรายปฏิบัติการ

Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Apache Kafka & Stream Processing Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Is a Serde?

A Serde is a combined Serializer and Deserializer. Kafka Streams uses it to convert between your Java objects and the bytes stored in Kafka.

Every key and value flowing through a topology needs a Serde.

Why Streams Needs Serdes Everywhere

Unlike a plain consumer, Kafka Streams writes intermediate results back to Kafka (for repartitioning and state).

So it must know how to serialize data not just on input/output, but at every shuffle and store — hence Serdes appear throughout the API.

Built-in Serdes

The Serdes factory class provides ready-made Serdes for common types.

Serde<String> stringSerde = Serdes.String();
Serde<Long> longSerde = Serdes.Long();
Serde<byte[]> bytesSerde = Serdes.ByteArray();

Default Serdes

You can set default key and value Serdes in the Streams config so you don't repeat them everywhere.

Properties props = new Properties();
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG,
    Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
    Serdes.String().getClass());

Overriding Per Operation

When a specific stream uses a different type, override the default with Consumed, Produced, or Materialized.

KStream<String, Long> counts = builder.stream(
    "counts-topic",
    Consumed.with(Serdes.String(), Serdes.Long()));

Custom JSON Serde

For domain objects, a common choice is a JSON Serde that maps a class to and from JSON bytes.

// Conceptual JSON value Serde for an Order POJO
Serde<Order> orderSerde = new JsonSerde<>(Order.class);

KStream<String, Order> orders = builder.stream(
    "orders",
    Consumed.with(Serdes.String(), orderSerde));

Avro & Schema Registry Serdes

For schema-governed data, use the Confluent SpecificAvroSerde or GenericAvroSerde.

  • They register and look up schemas in Schema Registry.
  • They enforce compatibility as your data evolves.

Serdes and Repartitioning

Operations like selectKey or groupBy trigger a repartition through an internal topic.

Kafka Streams needs valid key/value Serdes at that point — a missing or wrong Serde here is a frequent source of runtime errors.

Serdes for State Stores

Stateful operations materialize results into a store backed by a changelog topic.

KTable<String, Long> totals = orders
    .groupByKey()
    .count(Materialized.with(Serdes.String(), Serdes.Long()));

Common Serde Errors

Watch for these pitfalls:

  • ClassCastException — the default Serde doesn't match the actual type.
  • SerializationException — bytes don't match the deserializer (wrong topic data).
  • Forgetting to override the Serde after changing the value type.

Best Practices

Keep serialization sane:

  • Set sensible defaults, override only when types change.
  • Use Schema Registry Serdes for evolving data contracts.
  • Make custom Serdes null-safe.
  • Match Serde types exactly at repartition and store boundaries.

Quick Check

Test your understanding of Serdes.

Recap

You learned how Serdes drive serialization in Kafka Streams.

  • A Serde bundles a serializer and deserializer.
  • Set defaults in config; override with Consumed/Produced/Materialized.
  • Use JSON or Avro/Schema Registry Serdes for domain objects.
  • Mismatched Serdes are a top cause of runtime errors.

คำถามที่พบบ่อย

บทเรียน “Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Apache Kafka & Stream Processing Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams”

เรียนรู้ว่า Kafka Streams ใช้ Serdes เพื่อทำให้คีย์และค่าเป็นอนุกรมและย้อนกลับจากอนุกรมอย่างไร รวมถึงวิธีกำหนดค่า Serdes เริ่มต้นและรายปฏิบัติการ คุณปฏิบัติ Apache Kafka & Stream Processing Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Apache Kafka & Stream Processing Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Apache Kafka & Stream Processing Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม

ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างแอป Kafka Streams อย่างง่าย
  2. แนวคิด KStream และ KTable
  3. การดำเนินการแบบไร้สถานะเทียบกับแบบมีสถานะ
  4. Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams
← กลับไปที่ Apache Kafka & Stream Processing Fundamentals