-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathstream_arbitrary_format_rows.rs
33 lines (30 loc) · 1.25 KB
/
stream_arbitrary_format_rows.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
use tokio::io::AsyncBufReadExt;
use clickhouse::Client;
/// An example of streaming raw data in an arbitrary format leveraging the
/// [`AsyncBufReadExt`] helpers. In this case, the format is `JSONEachRow`.
/// Incoming data is then split into lines, and each line is deserialized into
/// `serde_json::Value`, a dynamic representation of JSON values.
///
/// Similarly, it can be used with other formats such as CSV, TSV, and others
/// that produce each row on a new line; the only difference will be in how the
/// data is parsed. See also: https://clickhouse.com/docs/en/interfaces/formats
///
/// Note: `lines()` produces a new `String` for each line, so it's not the
/// most performant way to interate over lines.
#[tokio::main]
async fn main() {
let client = Client::default().with_url("http://localhost:8123");
let mut lines = client
.query(
"SELECT number, hex(randomPrintableASCII(20)) AS hex_str
FROM system.numbers
LIMIT 100",
)
.fetch_bytes("JSONEachRow")
.unwrap()
.lines();
while let Some(line) = lines.next_line().await.unwrap() {
let value: serde_json::Value = serde_json::de::from_str(&line).unwrap();
println!("JSONEachRow value: {}", value);
}
}