forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_list.rs
69 lines (55 loc) · 1.72 KB
/
value_list.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
use anyhow::Result;
use futures::StreamExt;
use scylla::{Session, 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.unwrap();
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.my_type (k int, my text, primary key (k))",
&[],
)
.await?;
#[derive(scylla::SerializeRow)]
struct MyType<'a> {
k: i32,
my: Option<&'a str>,
}
let to_insert = MyType {
k: 17,
my: Some("Some str"),
};
session
.query_unpaged(
"INSERT INTO examples_ks.my_type (k, my) VALUES (?, ?)",
to_insert,
)
.await?;
// You can also use type generics:
#[derive(scylla::SerializeRow)]
struct MyTypeWithGenerics<S: scylla::serialize::value::SerializeValue> {
k: i32,
my: Option<S>,
}
let to_insert_2 = MyTypeWithGenerics {
k: 18,
my: Some("Some string".to_owned()),
};
session
.query_unpaged(
"INSERT INTO examples_ks.my_type (k, my) VALUES (?, ?)",
to_insert_2,
)
.await?;
let iter = session
.query_iter("SELECT * FROM examples_ks.my_type", &[])
.await?
.rows_stream::<(i32, String)>()?;
let rows = iter.collect::<Vec<_>>().await;
println!("Q: {:?}", rows);
Ok(())
}