Expand description

Async Rust driver for the Scylla database written in Rust. Although optimized for Scylla, the driver is also compatible with Apache Cassandra®.

Documentation book

The best source to learn about this driver is the documentation book.
This page contains mainly API documentation

Other documentation

Driver overview

Connecting

All driver activity revolves around the Session
Session is created by specifying a few known nodes and connecting to them:

use scylla::{Session, SessionBuilder};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
   let session: Session = SessionBuilder::new()
        .known_node("127.0.0.1:9042")
        .known_node("1.2.3.4:9876")
        .build()
        .await?;

   Ok(())
}

Session is usually created using the SessionBuilder.
All configuration options for a Session can be specified while building.

Making queries

After successfully connecting to the cluster we can make queries.
The driver supports multiple query types:

To specify options for a single query create the query object and configure it:

The easiest way to specify bound values in a query is using a tuple:

// Insert an int and text into the table
session
    .query(
        "INSERT INTO ks.tab (a, b) VALUES(?, ?)",
        (2_i32, "some text")
    )
    .await?;

But the driver will accept anything implementing the trait ValueList

Receiving results

The easiest way to read rows returned by a query is to cast each row to a tuple of values:

use scylla::IntoTypedRows;

// Read rows containing an int and text
let rows_opt = session
.query("SELECT a, b FROM ks.tab", &[])
    .await?
    .rows;

if let Some(rows) = rows_opt {
    for row in rows.into_typed::<(i32, String)>() {
        // Parse row as int and text \
        let (int_val, text_val): (i32, String) = row?;
    }
}

See the book for more receiving methods

Re-exports

pub use statement::batch;
pub use statement::prepared_statement;
pub use statement::query;
pub use transport::query_result::QueryResult;
pub use transport::session::IntoTypedRows;
pub use transport::session::Session;
pub use transport::session::SessionConfig;
pub use transport::session_builder::SessionBuilder;
pub use transport::load_balancing;
pub use transport::retry_policy;
pub use transport::speculative_execution;

Modules

Structs

Result of Session::batch(). Contains no rows, only some useful information.

A cheaply cloneable and sliceable chunk of contiguous memory.

A unique reference to a contiguous slice of memory.

Provides auto caching while executing queries

Traits

A trait for values that provide sequential write access to bytes.

This trait defines a way to convert CQL Row into some rust type

Derive Macros

#[derive(FromRow)] derives FromRow for struct Works only on simple structs without generics etc

#[derive(FromUserType)] allows to parse a struct as User Defined Type Works only on simple structs without generics etc

#[derive(IntoUserType)] allows to parse a struct as User Defined Type Works only on simple structs without generics etc

#[derive(ValueList)] derives ValueList for struct Works only on simple structs without generics etc