pub struct ProducerRecord<K, V> { /* private fields */ }Expand description
A key/value pair to be sent to Kafka. This consists of a topic name to which the record is being sent, an optional partition number, and an optional key and value.
If a valid partition number is specified that partition will be used when sending the record. If no partition is specified but a key is present a partition will be chosen using a hash of the key. If neither key nor partition is present a partition will be assigned in a round-robin fashion. Note that partition numbers are 0-indexed.
The record also has an associated timestamp. If the user did not provide a timestamp, the producer will stamp the record with its current time. The timestamp eventually used by Kafka depends on the timestamp type configured for the topic.
- If the topic is configured to use
CreateTime, the timestamp in the producer record will be used by the broker. - If the topic is configured to use
LogAppendTime, the timestamp in the producer record will be overwritten by the broker with the broker local time when it appends the message to its log.
In either case, the timestamp that has actually been used will be returned to the user
in RecordMetadata.
Implementations§
Source§impl<K, V> ProducerRecord<K, V>
impl<K, V> ProducerRecord<K, V>
Sourcepub fn with_options(
options: ProducerRecordOptions<K, V>,
) -> Result<Self, LocalIllegalArgumentError>
pub fn with_options( options: ProducerRecordOptions<K, V>, ) -> Result<Self, LocalIllegalArgumentError>
Creates a record with a specified timestamp to be sent to a specified topic and partition.
§Arguments
options- Every parameter of Java’s widest constructor: topic, partition, timestamp, key, value and headers
Corresponds to Java’s ProducerRecord(String, Integer, Long, K, V, Iterable<Header>)
(ProducerRecord.java:69). Its six parameters exceed CLAUDE.md §2’s
three-parameter cap on derived overload names, so
ProducerRecordOptions is this method’s only parameter.
§Errors
Returns an error if:
- The timestamp is negative
- The partition is negative
Sourcepub fn with_partition_timestamp_key(
topic: String,
partition: Option<i32>,
timestamp: Option<i64>,
key: Option<K>,
value: Option<V>,
) -> Result<Self, LocalIllegalArgumentError>
pub fn with_partition_timestamp_key( topic: String, partition: Option<i32>, timestamp: Option<i64>, key: Option<K>, value: Option<V>, ) -> Result<Self, LocalIllegalArgumentError>
Creates a record with a specified timestamp to be sent to a specified topic and partition (without headers).
Corresponds to Java’s ProducerRecord(String, Integer, Long, K, V)
(ProducerRecord.java:96).
§Errors
Returns an error if the timestamp is negative or the partition is negative.
Sourcepub fn with_partition_key_headers(
topic: String,
partition: Option<i32>,
key: Option<K>,
value: Option<V>,
headers: RecordHeaders,
) -> Result<Self, LocalIllegalArgumentError>
pub fn with_partition_key_headers( topic: String, partition: Option<i32>, key: Option<K>, value: Option<V>, headers: RecordHeaders, ) -> Result<Self, LocalIllegalArgumentError>
Creates a record to be sent to a specified topic and partition (with headers, no timestamp).
Corresponds to Java’s ProducerRecord(String, Integer, K, V, Iterable<Header>)
(ProducerRecord.java:109).
§Errors
Returns an error if the partition is negative.
Sourcepub fn with_partition_key(
topic: String,
partition: Option<i32>,
key: Option<K>,
value: Option<V>,
) -> Result<Self, LocalIllegalArgumentError>
pub fn with_partition_key( topic: String, partition: Option<i32>, key: Option<K>, value: Option<V>, ) -> Result<Self, LocalIllegalArgumentError>
Creates a record to be sent to a specified topic and partition.
Corresponds to Java’s ProducerRecord(String, Integer, K, V)
(ProducerRecord.java:121).
§Errors
Returns an error if the partition is negative.
Sourcepub fn with_key(topic: String, key: Option<K>, value: Option<V>) -> Self
pub fn with_key(topic: String, key: Option<K>, value: Option<V>) -> Self
Creates a record to be sent to Kafka with a key and value (no partition, no timestamp, no headers).
Corresponds to Java’s ProducerRecord(String, K, V) (ProducerRecord.java:132).
Sourcepub fn new(topic: String, value: Option<V>) -> Self
pub fn new(topic: String, value: Option<V>) -> Self
Creates a record with no key (no partition, no timestamp, no headers).
Corresponds to Java’s ProducerRecord(String, V) (ProducerRecord.java:142),
whose parameters {topic, value} are the intersection across the six
constructors — so it owns the plain name (CLAUDE.md §2).
Sourcepub fn headers(&self) -> &RecordHeaders
pub fn headers(&self) -> &RecordHeaders
Returns the headers.
Sourcepub fn headers_mut(&mut self) -> &mut RecordHeaders
pub fn headers_mut(&mut self) -> &mut RecordHeaders
Returns a mutable reference to the headers.
Sourcepub fn timestamp(&self) -> Option<i64>
pub fn timestamp(&self) -> Option<i64>
Returns the timestamp, which is in milliseconds since epoch.
Sourcepub fn partition(&self) -> Option<i32>
pub fn partition(&self) -> Option<i32>
Returns the partition to which the record will be sent (or None if no partition
was specified).
Sourcepub fn into_parts(
self,
) -> (String, Option<i32>, Option<i64>, RecordHeaders, Option<K>, Option<V>)
pub fn into_parts( self, ) -> (String, Option<i32>, Option<i64>, RecordHeaders, Option<K>, Option<V>)
Consume this record and return its parts.
Trait Implementations§
Source§impl<K: Clone, V: Clone> Clone for ProducerRecord<K, V>
impl<K: Clone, V: Clone> Clone for ProducerRecord<K, V>
Source§fn clone(&self) -> ProducerRecord<K, V>
fn clone(&self) -> ProducerRecord<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<K: Eq, V: Eq> Eq for ProducerRecord<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for ProducerRecord<K, V>
impl<K, V> RefUnwindSafe for ProducerRecord<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for ProducerRecord<K, V>
impl<K, V> Sync for ProducerRecord<K, V>
impl<K, V> Unpin for ProducerRecord<K, V>
impl<K, V> UnsafeUnpin for ProducerRecord<K, V>where
K: UnsafeUnpin,
V: UnsafeUnpin,
impl<K, V> UnwindSafe for ProducerRecord<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.