liveBook · Manning

Map

As explained in Apache Kafka in Action

map is a stream-processing function that transforms each input record into a new record or value.

A map processor transforms every input message into a new message. Its function receives the original key and value and returns a new key and value, making it suitable for reshaping raw stream data or changing record identity. Use mapValues() instead when only the value changes; preserving the key avoids the repartitioning and performance cost that often follows key changes. section 12.2.1

Choose the processor according to the part of the record that must change:

  • Use map() to transform both fields, such as turning an unkeyed value {id: "Apple", price: 1.50} into key Apple and value 1.50. section 12.2.1
  • Use mapValues() to convert, mask, or transform values while retaining the key, such as converting prices from dollars to cents. section 12.2.1
  • Place transformed records into another Kafka topic with to(topicName), forming part of a larger processor topology. section 12.2.1 section 12.7

Creating a key from a raw price record

Starting with a value such as {id: "Apple", price: 1.50} and no key, map((key, value) -> (value.id, value.price)) produces a record keyed by Apple with value 1.50. If the key already identifies the record and only the price needs conversion, mapValues((key, value) -> value.price * 100) changes the value without triggering a key change. section 12.2.1

Figure 12.2 The filter and map processors
A group of glowing objects

AI-generated content may be incorrect.

Use the processor diagram to compare filter(), which selects records, with map(), which transforms their key-value representation. section 12.2.1

How Apache Kafka in Action covers it

How deep it goes
Defines input/output signatures, contrasts mapValues(), and explains repartitioning-related performance loss without detailing implementation internals.
Examples
Demonstrates dollar-to-cent conversion with mapValues() and key creation from an unkeyed Apple price record.
What you can do after
Apply it via Kafka Streams to transform records, create keys, and write the resulting stream to a topic.
How it connects
Connects to filter(), mapValues(), processor topologies, repartitioning, Kafka Streams, and Flink.

Also explained in

Compare how 8 books explain map →

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.