forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.rs
106 lines (91 loc) · 3.29 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
106
use anyhow::Result;
use futures::StreamExt as _;
use futures::TryStreamExt as _;
use scylla::frame::response::result::Row;
use scylla::transport::session::Session;
use scylla::DeserializeRow;
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_unpaged("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query_unpaged(
"CREATE TABLE IF NOT EXISTS examples_ks.basic (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
session
.query_unpaged(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;
session
.query_unpaged(
"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_unpaged(&prepared, (42_i32, "I'm prepared!"))
.await?;
session
.execute_unpaged(&prepared, (43_i32, "I'm prepared 2!"))
.await?;
session
.execute_unpaged(&prepared, (44_i32, "I'm prepared 3!"))
.await?;
// Rows can be parsed as tuples
let mut iter = session
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows_stream::<(i32, i32, String)>()?;
while let Some((a, b, c)) = iter.try_next().await? {
println!("a, b, c: {}, {}, {}", a, b, c);
}
// Or as custom structs that derive DeserializeRow
#[allow(unused)]
#[derive(Debug, DeserializeRow)]
struct RowData {
a: i32,
b: Option<i32>,
c: String,
}
let mut iter = session
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows_stream::<RowData>()?;
while let Some(row_data) = iter.try_next().await? {
println!("row_data: {:?}", row_data);
}
// Or simply as untyped rows
let mut iter = session
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows_stream::<Row>()?;
while let Some(row) = iter.next().await.transpose()? {
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);
}
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(())
}