LrKafkaProducer.newRecord

A fluent Builder pattern API for constructing producer records.

Note: This API function is supported from version 26.3.

public RecordBuilder newRecord(String topic)

Methods

MethodTypeDescription
.withValue(value)Required builder method Sets the record value.
.withKey(key) Optional builder methodSets the record key.
.withHeader(name, value) Optional builder method Adds a message header; repeatable for multiple headers.
.withPartition(partition) Optional builder methodPins the record to a specific partition.
.withTimestamp(timestamp) Optional builder methodSets the record timestamp in milliseconds.
.build()Terminal methodConstructs the final ProducerRecord.

Return Values

This function returns the ProducerRecord for the given topic.

General information

Both withPartition() and withTimestamp() are supported by the Builder, but are intentionally omitted during the code generation phase:

  • Partition. The original partition is specific to the cluster and topic configuration at recording time. Hard-coding it would make the script fail or produce skewed results, if the target cluster has a different number of partitions, or if key-based partitioning applies during replay. Omitting it lets Kafka assign partitions naturally during replay.

  • Timestamp. The recorded timestamp reflects the exact moment the message was produced during the recording session. Replaying a script with those timestamps would inject historical timestamps into the topic rather than current ones.

Both can be added manually to the generated script, if the test scenario specifically requires controlling partition assignment or timestamp values.