liveBook · Manning

selectKey

As explained in Kafka for Architects

selectKey is a Kafka Streams operation that assigns a new key to each input record and repartitions the resulting stream when necessary.

selectKey assigns a new key to each input event so related records can reach matching Kafka Streams tasks for a join. section 8.3.6

Use selectKey when stream records carry different keys from the attribute required for correlation:

  • Change a Payment event's key from paymentId to its orderId attribute. section 8.3.6
  • Let Kafka Streams create an intermediate topic with the new keys, aligning records for downstream processing. section 8.3.6

Orders and Payments

An Orders topic uses orderId while a Payments topic uses paymentId. Applying selectKey to Payments with orderId makes both streams joinable, provided their partitioning is aligned. section 8.3.6

How Kafka for Architects covers it

How deep it goes
Defines key reassignment and intermediate-topic creation through an Orders-Payments join scenario.
Examples
Uses Orders keyed by orderId and Payments keyed by paymentId, then rekeys Payments to orderId.
What it assumes
Assumes understanding of event keys, partitions, Kafka Streams tasks, and stream joins.
What you can do after
Apply it via the Kafka Streams API to rekey records for a downstream join.
How it connects
Connects key reassignment with stream joins, partitions, intermediate topics, and sliding windows.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka for Architects. The section links point to the parts of the book it draws on.