'Send json data to Kafka producer in Rust

I have been following Rust kafka producer example, which looks like ..

use std::fmt::Write;
use std::time::Duration;
use kafka::producer::{Producer, Record, RequiredAcks};

let mut producer =
    Producer::from_hosts(vec!("localhost:9092".to_owned()))
        .with_ack_timeout(Duration::from_secs(1))
        .with_required_acks(RequiredAcks::One)
        .create()
        .unwrap();

let mut buf = String::with_capacity(2);
for i in 0..10 {
  let _ = write!(&mut buf, "{}", i); // some computation of the message data to be sent
  producer.send(&Record::from_value("my-topic", buf.as_bytes())).unwrap();
  buf.clear();
}

This example simply sends numbers from 1 to 10, I can modify this to send strings, but how can I modify this example to send a json, something like ...

{
  "cpu": {
     "system": "40",
     "user": "30",
  },
  "ram": 40, 
}

Should I create a struct, stringify it & then again objectify at kafka consumer ? OR does Kafka supports different formats ?

I tried serializing and sending the data as follows ...

use serde::{Deserialize, Serialize};
use std::time::Duration;
use kafka::producer::{Producer, Record, RequiredAcks};

#[derive(Serialize, Deserialize)]
struct Data {
    cpu: f32,
    memory: f32,
}

fn main() {
    let mut producer =
        Producer::from_hosts(vec!("localhost:29092".to_owned()))
            .with_ack_timeout(Duration::from_secs(1))
            .with_required_acks(RequiredAcks::One)
            .create()
            .unwrap();

            let data = Data{
                cpu: 80.0,
                memory: 40.0,
            };

    let mut buf = String::with_capacity(2);
    for _ in 0..10 {
    // let _ = write!(&mut buf, "{}", i); // some computation of the message data to be sent
    producer.send(&Record::from_value("Users", bincode::serialize(&data).unwrap())).unwrap();
    buf.clear();
    }
}

But, at the consumer end (which is written in nodejs), I receive something like this ..

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source