-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_producer.rs
54 lines (42 loc) · 1.49 KB
/
json_producer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use anyhow::Result;
use danube_client::{DanubeClient, SchemaType};
use serde_json::json;
use std::thread;
use std::time::Duration;
use tracing::info;
#[tokio::main]
async fn main() -> Result<()> {
// Setup tracing
tracing_subscriber::fmt::init();
let client = DanubeClient::builder()
.service_url("http://127.0.0.1:6650")
.build()
.unwrap();
let topic = "/default/test_topic";
let producer_name = "prod_json";
let json_schema = r#"{"type": "object", "properties": {"field1": {"type": "string"}, "field2": {"type": "integer"}}}"#.to_string();
let mut producer = client
.new_producer()
.with_topic(topic)
.with_name(producer_name)
.with_schema("my_app".into(), SchemaType::Json(json_schema))
.build();
producer.create().await?;
info!("The Producer {} was created", producer_name);
let mut i = 0;
while i < 100 {
let data = json!({
"field1": format!{"value{}", i},
"field2": 2020+i,
});
// Convert to string and encode to bytes
let json_string = serde_json::to_string(&data).unwrap();
let encoded_data = json_string.as_bytes().to_vec();
// let json_message = r#"{"field1": "value", "field2": 123}"#.as_bytes().to_vec();
let message_id = producer.send(encoded_data, None).await?;
println!("The Message with id {} was sent", message_id);
thread::sleep(Duration::from_secs(1));
i += 1;
}
Ok(())
}