Last active
May 25, 2024 00:52
-
-
Save diegofcornejo/11aa2cb2b1df5d06f87ef818f2a9a412 to your computer and use it in GitHub Desktop.
Cassandra - Rust example (scylladb/scylla-rust-driver)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use scylla::{SessionBuilder, IntoTypedRows}; | |
use std::error::Error; | |
use std::env; | |
use std::time::Instant; | |
use dotenv::dotenv; | |
const QUERY_LIMIT: i32 = 5000; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn Error>> { | |
// Load environment variables from .env file | |
dotenv().ok(); | |
let nodes = env::var("DB_NODES") | |
.expect("DB_NODES must be set") | |
.split(',') | |
.map(String::from) | |
.collect::<Vec<String>>(); | |
let username = env::var("DB_USERNAME").expect("DB_USERNAME must be set"); | |
let password = env::var("DB_PASSWORD").expect("DB_PASSWORD must be set"); | |
let session = SessionBuilder::new() | |
.known_nodes(&nodes) | |
.user(&username, &password) | |
.build() | |
.await?; | |
let start = Instant::now(); | |
let rows = session | |
.query("SELECT id FROM gridia.event LIMIT ?", (QUERY_LIMIT,)) | |
.await? | |
.rows | |
.unwrap() | |
.into_typed::<(i32,)>(); | |
let duration = start.elapsed(); | |
let count = rows.count(); | |
println!("Query executed in: {:?}", duration); | |
println!("Number of rows returned: {}", count); | |
Ok(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DB_NODES=db01.cloud.myserver.com:9042,db01.cloud.myserver.com:9043,db01.cloud.myserver.com:9044 | |
DB_USERNAME=cassandra | |
DB_PASSWORD=cassandra |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment