Serdes i serializacja danych w Kafka Streams
Nauczą się Państwo, jak Kafka Streams używa Serdes do serializacji i deserializacji kluczy oraz wartości, a także jak konfigurować domyślne i przypisane do operacji Serdes.
Serdes i serializacja danych w Kafka Streams to bezpłatna lekcja Apache Kafka & Stream Processing Fundamentals na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Apache Kafka & Stream Processing Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Apache Kafka & Stream Processing Fundamentals zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Serdes i serializacja danych w Kafka Streams” jest bezpłatna?
Tak — pełny tekst „Serdes i serializacja danych w Kafka Streams” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Apache Kafka & Stream Processing Fundamentals, przejdź na CoddyKit PRO. Kurs Apache Kafka & Stream Processing Fundamentals zawiera 4 lekcji w sumie.
Co nauczysz się w „Serdes i serializacja danych w Kafka Streams”?
Nauczą się Państwo, jak Kafka Streams używa Serdes do serializacji i deserializacji kluczy oraz wartości, a także jak konfigurować domyślne i przypisane do operacji Serdes. Ćwiczysz Apache Kafka & Stream Processing Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Apache Kafka & Stream Processing Fundamentals?
Nie wymagamy żadnego doświadczenia. Apache Kafka & Stream Processing Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Serdes i serializacja danych w Kafka Streams”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Apache Kafka & Stream Processing Fundamentals?
Tak. Każda lekcja Apache Kafka & Stream Processing Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Tworzenie prostej aplikacji Kafka Streams
- Pojęcia KStream i KTable
- Operacje bezstanowe a stanowe
- Serdes i serializacja danych w Kafka Streams