liveBook · Manning

mapValues

As explained in Apache Kafka in Action

mapValues is a stream-processing function that transforms record values while preserving their keys.

mapValues transforms each stream record’s value while preserving its key. Use it to convert, mask, or otherwise reshape values without triggering the key-changing behavior of map(). section 12.2.1

Convert prices without changing keys

A price stream can apply mapValues((key, value) -> value.price * 100) to convert dollars to cents. Each record keeps its original key while its price value becomes cents. section 12.2.1

Choose mapValues when the value changes but the record identity represented by the key should remain stable:

  • Prefer it over map() when only the value changes; changing keys can require repartitioning and cause performance losses. section 12.2.1
  • After an average aggregation, use it to expose the calculated average instead of retaining the internal sum and count state. section 12.4.2

How Apache Kafka in Action covers it

How deep it goes
Defines value-only transformation, demonstrates price conversion, and explains repartitioning consequences without detailing processor internals.
Examples
Demonstrates converting prices from dollars to cents and projecting averages from aggregate state.
What you can do after
Apply via Kafka Streams to transform values without changing keys or unnecessarily causing repartitioning.
How it connects
Contrasts with filter and map, and connects to repartitioning and aggregation state.

Also explained in

Compare how 2 books explain mapValues →

Related concepts

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