Apache Kafka & Stream Processing Fundamentals · レッスン

Kafka StreamsのSerdesとデータシリアライゼーション

Kafka StreamsがSerdesでキーと値をシリアライズ・デシリアライズする方法と、デフォルトおよび操作ごとのSerdesを設定する方法を学びます。

レッスン 4/413 ステップ

「Kafka StreamsのSerdesとデータシリアライゼーション」はCoddyKit上の無料Apache Kafka & Stream Processing Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
無料で開始

AI チューターと学ぶ Apache Kafka & Stream Processing Fundamentals — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「Kafka StreamsのSerdesとデータシリアライゼーション」レッスンは無料ですか?

はい。「Kafka StreamsのSerdesとデータシリアライゼーション」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Apache Kafka & Stream Processing Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Apache Kafka & Stream Processing Fundamentalsコースには全4レッスンが含まれています。

「Kafka StreamsのSerdesとデータシリアライゼーション」で何を学びますか?

Kafka StreamsがSerdesでキーと値をシリアライズ・デシリアライズする方法と、デフォルトおよび操作ごとのSerdesを設定する方法を学びます。 ブラウザで直接実行するハンズオンコードでApache Kafka & Stream Processing Fundamentalsを演習し、24時間対応の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に戻る