rust-rdkafka icon indicating copy to clipboard operation
rust-rdkafka copied to clipboard

How to set BaseRecord key as none

Open siweilvxing opened this issue 1 year ago • 1 comments

I don't want set key in BaseRecord like this

let br = BaseRecord::with_opaque_to("flow-file-info", ()).payload(fi);

but hint

impl<'a, K: ToBytes + ?Sized, P: ToBytes + ?Sized, D: IntoOpaque> BaseRecord<'a, K, P, D> {
                ^^^^^^^ required by this bound in `BaseRecord::<'a, K, P, D>::with_opaque_to`

so, I fix like this

let br = BaseRecord::with_opaque_to("flow-file-info", ()).payload(fi).key("");

how do I set key as none?

siweilvxing avatar Nov 27 '23 13:11 siweilvxing

Take a look at the BaseRecord definition:

pub struct BaseRecord<'a, K: ToBytes + ?Sized = (), P: ToBytes + ?Sized = (), D: IntoOpaque = ()> {
    /// Required destination topic.
    pub topic: &'a str,
    /// Optional destination partition.
    pub partition: Option<i32>,
    /// Optional payload.
    pub payload: Option<&'a P>,
    /// Optional key.
    pub key: Option<&'a K>,
    /// Optional timestamp.
    ///
    /// Note that Kafka represents timestamps as the number of milliseconds
    /// since the Unix epoch.
    pub timestamp: Option<i64>,
    /// Optional message headers.
    pub headers: Option<OwnedHeaders>,
    /// Required delivery opaque (defaults to `()` if not required).
    pub delivery_opaque: D,
}

It takes 3 generics and defaults to () when you dont set them.

You can just not set your .key(...) and specify your type. Also, opaque is () by default if you don't want to use it just use BaseRecord::to(topic).

let br: BaseRecord<'_, (), [u8]> = BaseRecord::to(topic).payload(&payload[..]);

FaveroFerreira avatar Dec 12 '23 22:12 FaveroFerreira