Skip to content

Cassandra driver for Rust which utilizes the DataStax C/C++ driver

Notifications You must be signed in to change notification settings

manenko/cassander

Repository files navigation

Cassander

Cassander provides Rust developers with a wrapper for the DataStax C/C++ Driver, enabling interaction with Apache Cassandra and DataStax products. Currently under development, users should anticipate possible changes and some bugs.

The library was tested against C/C++ driver version 2.17.1, but should work with older versions too.

To get started, include Cassander in your Cargo.toml as follows:

[dependencies]
cassander = "0.1"

Prerequisites

Make sure you have the DataStax C/C++ Driver installed and available for dynamic loading.

Optional Features

Cassander comes with several optional features to enhance its functionality for specific use cases.

serde

The serde feature allows for serialization and deserialization of SessionConfig through the serde crate, facilitating the storage of driver configurations in a file for application loading.

Duration values are serialized as strings (e.g., 100ms, 2s, 1h10m) using the duration_string crate. These implementation details, however, should not be relied upon by the user.

uuid

This feature introduces conversions between Cassandra's CqlUuid and the Uuid from the uuid crate, simplifying the handling of UUID values.

chrono

The feature introduces conversions between Cassandra's date/time types and corresponding types from the chrono crate.

num-bigint

The feature introduces conversions between Cassandra's CqlVarInt and the BigInt from the num-bigint crate.

bigdecimal

The feature introduces conversions between Cassandra's CqlDecimal and the BigDecimal from the bigdecimal crate.

Examples

Create a session via SessionConfigBuilder

The SessionConfigBuilder facilitates creating connections to Cassandra clusters with custom settings:

use anyhow::Result;
use cassander::{SessionConfigBuilder, Authenticator};

#[tokio::main]
async fn main() -> Result<()> {
    let _session = SessionConfigBuilder::new()
        .contact_point("cassandra.testserver.com")
        .authenticator(Authenticator::plain_text("test_user", "password"))
        .keyspace("test_keyspace")
        .page_size(5000)
        .build()
        .connect()
        .await?;

    Ok(())
}

Deserializing SessionConfig from a TOML file

Update your application's Cargo.toml to include necessary dependencies and enable Cassander's serde feature:

[dependencies]
anyhow    = "1"
cassander = { version = "0.1", features = ["serde"] }
serde     = { version = "1", features = ["derive"] }
tokio     = { version = "1", features = ["full"] }
toml      = "0.8"

In your main.rs, deserialize the TOML configuration as follows:

use anyhow::Result;
use cassander::SessionConfig;
use serde::Deserialize;
use tokio::fs;

#[derive(Deserialize)]
struct Config {
    pub cassandra: SessionConfig
    // Additional configuration keys and sections
    // ...
}

#[tokio::main]
async fn main() -> Result<()> {
    let config_toml = fs::read_to_string("app.toml").await?;
    let config = toml::from_str(&config_toml)?;

    let _session = config.connect().await?;

    Ok(())
}

Example configuration file (app.toml):

[cassandra]
  contact_points      = ["127.0.0.1"]
  consistency         = "Quorum"
  reconnect_wait_time = "2554ms"
  page_size           = 5000
  application_name    = "Cassander Test Client"
  application_version = "0.0.1"
  client_id           = "37b9fd59-bc06-4a88-aaf3-bd829fd7b755"

[cassandra.authenticator.plain_text]
  username            = "test_user"
  password            = "secret"

About

Cassandra driver for Rust which utilizes the DataStax C/C++ driver

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages