liveBook · Manning

mapValues

As explained in Kafka Streams in Action, Second Edition

KStream.mapValues is a Kafka Streams operation that maps each record value to a new value while retaining the record key.

KStream.mapValues transforms the value of every record in a Kafka Streams KStream while forwarding the original key unchanged. It creates a child processing node in the topology: records flow from the upstream node into the mapping node, then on to later operations such as a sink. The mapper may return a different value type, but it should not modify the original value object. section 6.3.1 section 6.7

Pass mapValues a ValueMapper<V, V1>. Its single apply method makes a lambda a natural fit. For a stream of text records, the mapper can uppercase each value while retaining its associated key. section 6.3.1

Listing 6.4 Mapping incoming text to uppercase
KStream<String, String> upperCasedStream =
  simpleFirstStream.mapValues(value -> value.toUpperCase());

This minimal transformation inserts an uppercase-processing node between the source stream and any downstream sink. section 6.3.1

Use mapValues when the new value depends only on the current value. When value computation also needs the existing key, use a ValueMapperWithKey, which still preserves that key. When the operation must produce a new key as well as a new value, use KStream.map with a KeyValueMapper instead. section 6.4.1 section 6.7

A typical value-only transformation pipeline is:

  • Read keyed records from a Kafka topic into a KStream using appropriate Serdes. section 6.3.1
  • Call mapValues with a ValueMapper or lambda that creates the transformed value. section 6.3.1
  • Write the resulting KStream to an output topic with to(). section 6.3.1

Masking purchase data

A transaction stream can map each RetailPurchase to a copy with its credit-card number masked. The key remains the customer identifier, while the value stays a RetailPurchase object containing protected data. If masking rules depend on the customer identifier, switch to ValueMapperWithKey rather than mapValues. section 6.4.1

mapValues also fits localized unit conversion inside a branched flow. A proximity-sensor branch can convert readings from feet to meters, then merge with an updated sensor stream for output. The transformation changes values only; merge combines the resulting streams without guaranteeing ordering between them. section 6.6.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines mapper interfaces, key-preservation behavior, and topology placement, while deferring lambda-expression instruction to external documentation.
Examples
Demonstrates uppercasing text, masking RetailPurchase credit-card numbers, and converting proximity readings from feet to meters.
What you can do after
Apply it via Kafka Streams to transform each record value while retaining its existing key.
How it connects
Connects mapValues to KStream.map, ValueMapperWithKey, KeyValueMapper, Serdes, topology nodes, split, and merge.

Also explained in

Compare how 2 books explain mapValues →

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.