-
Notifications
You must be signed in to change notification settings - Fork 107
/
get_by_name.rs
62 lines (53 loc) · 1.88 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
55
56
57
58
59
60
61
62
use anyhow::{anyhow, Result};
use scylla::frame::response::result::Row;
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_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.get_by_name (pk int, ck int, value text, primary key (pk, ck))",
&[],
)
.await?;
session
.query_unpaged(
"INSERT INTO examples_ks.get_by_name (pk, ck, value) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;
session
.query_unpaged(
"INSERT INTO examples_ks.get_by_name (pk, ck, value) VALUES (1, 2, 'abc')",
&[],
)
.await?;
let rows_result = session
.query_unpaged("SELECT pk, ck, value FROM examples_ks.get_by_name", &[])
.await?
.into_rows_result()?;
let col_specs = rows_result.column_specs();
let (ck_idx, _) = col_specs
.get_by_name("ck")
.ok_or_else(|| anyhow!("No ck column found"))?;
let (value_idx, _) = col_specs
.get_by_name("value")
.ok_or_else(|| anyhow!("No value column found"))?;
let rows = rows_result
.rows::<Row>()
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap();
println!("ck | value");
println!("---------------------");
for row in rows {
println!("{:?} | {:?}", row.columns[ck_idx], row.columns[value_idx]);
}
Ok(())
}