forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_by_name.rs
54 lines (45 loc) · 1.6 KB
/
get_by_name.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::{anyhow, Result};
use scylla::transport::session::Session;
use scylla::SessionBuilder;
use std::env;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1".to_string());
println!("Connecting to {} ...", uri);
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query(
"CREATE TABLE IF NOT EXISTS ks.hello (pk int, ck int, value text, primary key (pk, ck))",
&[],
)
.await?;
session
.query(
"INSERT INTO ks.hello (pk, ck, value) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;
session
.query(
"INSERT INTO ks.hello (pk, ck, value) VALUES (1, 2, 'abc')",
&[],
)
.await?;
let query_result = session
.query("SELECT pk, ck, value FROM ks.hello", &[])
.await?;
let (ck_idx, _) = query_result
.get_column_spec("ck")
.ok_or_else(|| anyhow!("No ck column found"))?;
let (value_idx, _) = query_result
.get_column_spec("value")
.ok_or_else(|| anyhow!("No value column found"))?;
println!("ck | value");
println!("---------------------");
for row in query_result.rows.ok_or_else(|| anyhow!("no rows found"))? {
println!("{:?} | {:?}", row.columns[ck_idx], row.columns[value_idx]);
}
Ok(())
}