forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.rs
105 lines (89 loc) · 3.23 KB
/
basic.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use anyhow::Result;
use scylla::macros::FromRow;
use scylla::transport::session::Session;
use scylla::SessionBuilder;
use std::env;
#[tokio::main]
async fn main() -> Result<()> {
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} ...", uri);
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query(
"CREATE TABLE IF NOT EXISTS examples_ks.basic (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
session
.query(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;
session
.query(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (1, 2, 'abc')",
&[],
)
.await?;
let prepared = session
.prepare("INSERT INTO examples_ks.basic (a, b, c) VALUES (?, 7, ?)")
.await?;
session
.execute(&prepared, (42_i32, "I'm prepared!"))
.await?;
session
.execute(&prepared, (43_i32, "I'm prepared 2!"))
.await?;
session
.execute(&prepared, (44_i32, "I'm prepared 3!"))
.await?;
// Rows can be parsed as tuples
let result = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?;
let mut iter = result.rows_typed::<(i32, i32, String)>()?;
while let Some((a, b, c)) = iter.next().transpose()? {
println!("a, b, c: {}, {}, {}", a, b, c);
}
// Or as custom structs that derive FromRow
#[derive(Debug, FromRow)]
struct RowData {
_a: i32,
_b: Option<i32>,
_c: String,
}
let result = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?;
let mut iter = result.rows_typed::<RowData>()?;
while let Some(row_data) = iter.next().transpose()? {
println!("row_data: {:?}", row_data);
}
// Or simply as untyped rows
let result = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?;
let rows = result.rows.unwrap();
for row in rows {
let a = row.columns[0].as_ref().unwrap().as_int().unwrap();
let b = row.columns[1].as_ref().unwrap().as_int().unwrap();
let c = row.columns[2].as_ref().unwrap().as_text().unwrap();
println!("a, b, c: {}, {}, {}", a, b, c);
// Alternatively each row can be parsed individually
// let (a2, b2, c2) = row.into_typed::<(i32, i32, String)>() ?;
}
let metrics = session.get_metrics();
println!("Queries requested: {}", metrics.get_queries_num());
println!("Iter queries requested: {}", metrics.get_queries_iter_num());
println!("Errors occurred: {}", metrics.get_errors_num());
println!("Iter errors occurred: {}", metrics.get_errors_iter_num());
println!("Average latency: {}", metrics.get_latency_avg_ms().unwrap());
println!(
"99.9 latency percentile: {}",
metrics.get_latency_percentile_ms(99.9).unwrap()
);
println!("Ok.");
Ok(())
}