Skip to content

Commit c533641

Browse files
committed
example of displayer
1 parent 156356e commit c533641

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

examples/displayer.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use anyhow::Result;
2+
use futures::StreamExt as _;
3+
use futures::TryStreamExt as _;
4+
use scylla::frame::response::result::Row;
5+
use scylla::transport::session::Session;
6+
use scylla::DeserializeRow;
7+
use scylla::QueryRowsResult;
8+
use scylla::SessionBuilder;
9+
use tracing_subscriber::field::display;
10+
use std::env;
11+
12+
#[tokio::main]
13+
async fn main() -> Result<()> {
14+
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
15+
16+
println!("Connecting to {} ...", uri);
17+
18+
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
19+
20+
session.query_unpaged("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
21+
22+
session
23+
.query_unpaged(
24+
"CREATE TABLE IF NOT EXISTS examples_ks.basic (a int, b int, c text, primary key (a, b))",
25+
&[],
26+
)
27+
.await?;
28+
29+
session
30+
.query_unpaged(
31+
"INSERT INTO examples_ks.basic (a, b, c) VALUES (?, ?, ?)",
32+
(3, 4, "def"),
33+
)
34+
.await?;
35+
36+
session
37+
.query_unpaged(
38+
"INSERT INTO examples_ks.basic (a, b, c) VALUES (1, 2, 'abc')",
39+
&[],
40+
)
41+
.await?;
42+
43+
let prepared = session
44+
.prepare("INSERT INTO examples_ks.basic (a, b, c) VALUES (?, 7, ?)")
45+
.await?;
46+
session
47+
.execute_unpaged(&prepared, (42_i32, "I'm prepared!"))
48+
.await?;
49+
session
50+
.execute_unpaged(&prepared, (43_i32, "I'm prepared 2!"))
51+
.await?;
52+
session
53+
.execute_unpaged(&prepared, (44_i32, "I'm prepared 3!"))
54+
.await?;
55+
56+
// Rows can be parsed as tuples
57+
let mut iter = session
58+
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
59+
.await?
60+
.rows_stream::<(i32, i32, String)>()?;
61+
while let Some((a, b, c)) = iter.try_next().await? {
62+
println!("a, b, c: {}, {}, {}", a, b, c);
63+
}
64+
65+
// Or as custom structs that derive DeserializeRow
66+
#[allow(unused)]
67+
#[derive(Debug, DeserializeRow)]
68+
struct RowData {
69+
a: i32,
70+
b: Option<i32>,
71+
c: String,
72+
}
73+
74+
let result : QueryRowsResult= session
75+
.query_unpaged("SELECT a, b, c FROM examples_ks.basic", &[])
76+
.await?
77+
.into_rows_result()?;
78+
79+
let displayer = result.rows_displayer();
80+
println!("DISPLAYER:");
81+
println!("{}", displayer);
82+
83+
Ok(())
84+
}

0 commit comments

Comments
 (0)