suharev7 / clickhouse-rs

Asynchronous ClickHouse client library for Rust programming language.
MIT License
324 stars 121 forks source link
clickhouse clickhouse-client rust tokio

Async ClickHouse Client

Build Status Crate info Documentation dependency status Coverage Status

Asynchronous Yandex ClickHouse client library for rust programming language.

Installation

Library hosted on crates.io.

[dependencies]
clickhouse-rs = "*"

Supported data types

DNS

schema://user:password@host[:port]/database?param1=value1&...&paramN=valueN

parameters:

example:

tcp://user:password@host:9000/clicks?compression=lz4&ping_timeout=42ms

Optional features

clickhouse-rs puts some functionality behind optional features to optimize compile time for the most common use cases. The following features are available.

Example

use clickhouse_rs::{Block, Pool};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let ddl = r"
        CREATE TABLE IF NOT EXISTS payment (
            customer_id  UInt32,
            amount       UInt32,
            account_name Nullable(FixedString(3))
        ) Engine=Memory";

    let block = Block::new()
        .column("customer_id",  vec![1_u32,  3,  5,  7,  9])
        .column("amount",       vec![2_u32,  4,  6,  8, 10])
        .column("account_name", vec![Some("foo"), None, None, None, Some("bar")]);

    let pool = Pool::new(database_url);

    let mut client = pool.get_handle().await?;
    client.execute(ddl).await?;
    client.insert("payment", block).await?;
    let block = client.query("SELECT * FROM payment").fetch_all().await?;

    for row in block.rows() {
        let id: u32             = row.get("customer_id")?;
        let amount: u32         = row.get("amount")?;
        let name: Option<&str>  = row.get("account_name")?;
        println!("Found payment {}: {} {:?}", id, amount, name);
    }

    Ok(())
}