Skip to main content

ProducerRecord

Struct ProducerRecord 

Source
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>

Source

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
Source

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.

Source

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.

Source

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.

Source

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).

Source

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).

Source

pub fn topic(&self) -> &str

Returns the topic this record is being sent to.

Source

pub fn headers(&self) -> &RecordHeaders

Returns the headers.

Source

pub fn headers_mut(&mut self) -> &mut RecordHeaders

Returns a mutable reference to the headers.

Source

pub fn key(&self) -> Option<&K>

Returns the key (or None if no key is specified).

Source

pub fn value(&self) -> Option<&V>

Returns the value.

Source

pub fn timestamp(&self) -> Option<i64>

Returns the timestamp, which is in milliseconds since epoch.

Source

pub fn partition(&self) -> Option<i32>

Returns the partition to which the record will be sent (or None if no partition was specified).

Source

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>

Source§

fn clone(&self) -> ProducerRecord<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for ProducerRecord<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Debug, V: Debug> Display for ProducerRecord<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash, V: Hash> Hash for ProducerRecord<K, V>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<K: PartialEq, V: PartialEq> PartialEq for ProducerRecord<K, V>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Eq, V: Eq> Eq for ProducerRecord<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for ProducerRecord<K, V>
where K: Freeze, V: Freeze,

§

impl<K, V> RefUnwindSafe for ProducerRecord<K, V>

§

impl<K, V> Send for ProducerRecord<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for ProducerRecord<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for ProducerRecord<K, V>
where K: Unpin, V: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V