liveBook · Manning

Key-changing operation

As explained in Kafka Streams in Action, Second Edition

A key-changing operation is a stream-processing operation that assigns or derives a different key for records.

A key-changing operation assigns or derives a different key for each record in a Kafka Streams topology. Operations such as map, flatMap, transform, and selectKey can change keys. Kafka Streams tracks this possibility and may repartition records before a later aggregation or join so records reach the partition for their new key. section 7.2.3

Use key-changing operations deliberately:

  • Change the key only when a downstream operation needs the new grouping or join key. section 7.2.3
  • Prefer value-only operators such as mapValues and flatMapValues when the key should remain unchanged, avoiding needless repartitioning. section 7.2.3
  • Use groupBy when changing the key before aggregation; otherwise prefer groupByKey. section 7.2.3
  • Reuse a repartitioned stream when subsequent aggregation and join operations need the same key, rather than creating redundant repartition topics. section 7.2.3
Figure 7.4 Repartitioning: Changing the original key to move records to a different partition
figure

Follow how changing the original key moves records to different partitions through repartitioning. section 7.2.3

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains the repartition flag, dependent operators, partition movement, and redundant repartition topics without detailing implementation internals.
Examples
Uses selectKey, groupByKey, count, join, map, filter, and repartition-topology examples.
What you can do after
Apply it via Kafka Streams while choosing operators that avoid unnecessary repartitioning and redundant repartition topics.
How it connects
Connects key changes with Kafka Streams, repartitioning, partitions, aggregation, joins, groupBy, and groupByKey.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.