liveBook · Manning

ProducerRecord

As explained in Akka in Action, Second Edition

ProducerRecord is a Kafka record type containing the data and destination topic information that a producer sends to Kafka.

ProducerRecord packages a value with its Kafka destination. A producer stream converts each element into a record, allowing messages to target one or several topics. section 14.2.1

Writing stream elements to Kafka

Configure ProducerSettings with serializers and a bootstrap server, then map stream values to records: Source(1 to 10).map(_.toString).map(elem => new ProducerRecord[String, String]("test", elem)).runWith(Producer.plainSink(producerSettings)). The sink consumes ProducerRecord values and completes with Future[Done]. section 14.2.1

class KafkaSpec ... {

 ...

 Source(1 to 10)
  .map(_.toString)
  .map(elem => new ProducerRecord[String, String]("test", elem))
  .runWith(Producer.plainSink(producerSettings))
 ...
}

A minimal Akka Streams construction shows the record’s generic key and value types, destination topic, and placement in the producer pipeline. section 14.2.1

Projection handlers use the same type for richer events: serialize a market event to Array[Byte], create a record with the topic, market identifier, and serialized value, then send it through SendProducer. The key supports event routing, while protobuf supplies the serialized payload. section 15.1.2

How Akka in Action, Second Edition covers it

How deep it goes
Explains construction with topic and value, then extends it to keyed, serialized projection events.
Examples
Uses a numbered stream sent to one topic and market events serialized into keyed projection records.
What it assumes
Assumes Kafka topics and producer settings while demonstrating serializers and record construction directly.
What you can do after
Apply it via Akka Streams or SendProducer to publish typed, serialized values to selected Kafka topics.
How it connects
Links Kafka producer settings, plainSink, SendProducer, event serialization, and projections.

Related concepts

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