Skip to main content

Rust Driver for Databend

The official Rust driver providing native connectivity with async/await support and comprehensive type safety for Rust applications.

Installation

Add the driver to your Cargo.toml:

[dependencies]
databend-driver = "0.7"
tokio = { version = "1", features = ["full"] }

Connection String: See Drivers Overview for DSN format and connection examples.


Key Features

  • Async/Await Support: Built for modern Rust async programming
  • Type Safety: Strong type mapping with Rust's type system
  • Connection Pooling: Efficient connection management
  • Stage Operations: Upload/download data to/from Databend stages
  • Streaming Results: Process large result sets efficiently

Data Type Mappings

Basic Types

DatabendRustNotes
BOOLEANbool
TINYINTi8, u8
SMALLINTi16, u16
INTi32, u32
BIGINTi64, u64
FLOATf32
DOUBLEf64
DECIMALStringPrecision preserved
VARCHARStringUTF-8 encoded
BINARYVec<u8>

Date/Time Types

DatabendRustNotes
DATEchrono::NaiveDateRequires chrono crate
TIMESTAMPchrono::NaiveDateTimeRequires chrono crate

Complex Types

DatabendRustNotes
ARRAY[T]Vec<T>Nested arrays supported
TUPLE[T, U](T, U)Multiple element tuples
MAP[K, V]HashMap<K, V>Key-value mappings
VARIANTStringJSON-encoded
BITMAPStringBase64-encoded
GEOMETRYStringWKT format

Basic Usage

Here's a simple example demonstrating DDL, write, and query operations:

use databend_driver::Client;
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to Databend
let client = Client::new("<your-dsn>".to_string());
let conn = client.get_conn().await?;

// DDL: Create table
conn.exec("CREATE TABLE IF NOT EXISTS users (id INT, name VARCHAR, created_at TIMESTAMP)")
.await?;

// Write: Insert data
conn.exec("INSERT INTO users VALUES (1, 'Alice', '2023-12-01 10:00:00')")
.await?;
conn.exec("INSERT INTO users VALUES (2, 'Bob', '2023-12-01 11:00:00')")
.await?;

// Query: Select data
let mut rows = conn.query_iter("SELECT id, name, created_at FROM users ORDER BY id")
.await?;

while let Some(row) = rows.next().await {
let (id, name, created_at): (i32, String, chrono::NaiveDateTime) =
row?.try_into()?;
println!("User {}: {} (created: {})", id, name, created_at);
}

Ok(())
}

Resources

Explore Databend Cloud for FREE
Low-cost
Fast Analytics
Easy Data Ingestion
Elastic Scaling
Try it today