forked from ClickHouse/clickhouse-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenums.rs
77 lines (66 loc) · 1.79 KB
/
enums.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::time::UNIX_EPOCH;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use clickhouse::{error::Result, Client, Row};
#[tokio::main]
async fn main() -> Result<()> {
let client = Client::default().with_url("http://localhost:8123");
client
.query("DROP TABLE IF EXISTS event_log")
.execute()
.await?;
client
.query(
"
CREATE TABLE event_log (
timestamp DateTime64(9),
message String,
level Enum8(
'Debug' = 1,
'Info' = 2,
'Warn' = 3,
'Error' = 4
)
)
ENGINE = MergeTree
ORDER BY timestamp",
)
.execute()
.await?;
#[derive(Debug, Serialize, Deserialize, Row)]
struct Event {
timestamp: u64,
message: String,
level: Level,
}
// How to define enums that map to `Enum8`/`Enum16`.
#[derive(Debug, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
enum Level {
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
}
let mut insert = client.insert("event_log")?;
insert
.write(&Event {
timestamp: now(),
message: "one".into(),
level: Level::Info,
})
.await?;
insert.end().await?;
let events = client
.query("SELECT ?fields FROM event_log")
.fetch_all::<Event>()
.await?;
println!("{:?}", events);
Ok(())
}
fn now() -> u64 {
UNIX_EPOCH
.elapsed()
.expect("invalid system time")
.as_nanos() as u64
}