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
Follow how changing the original key moves records to different partitions through repartitioning. section 7.2.3
