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

