0Pricing
Apache Kafka & Stream Processing Fundamentals · 课时

Kafka Streams 中的 Serdes 与数据序列化

学习 Kafka Streams 如何使用 Serdes 序列化和反序列化键与值,以及如何配置默认 Serdes 和按操作配置的 Serdes。

Kafka Streams 中的 Serdes 与数据序列化 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.

常见问题解答

「Kafka Streams 中的 Serdes 与数据序列化」课时是免费的吗?

是的 — 「Kafka Streams 中的 Serdes 与数据序列化」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。

「Kafka Streams 中的 Serdes 与数据序列化」这节课中我会学到什么?

学习 Kafka Streams 如何使用 Serdes 序列化和反序列化键与值,以及如何配置默认 Serdes 和按操作配置的 Serdes。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Kafka Streams 中的 Serdes 与数据序列化」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?

能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 构建简单的 Kafka Streams 应用
  2. KStream 和 KTable 概念
  3. 无状态操作与有状态操作
  4. Kafka Streams 中的 Serdes 与数据序列化
← 返回 Apache Kafka & Stream Processing Fundamentals