How to Connect Rust to Starwift ClickHouse: A Step‑by‑Step Guide
This article walks through setting up a Starwift cloud‑native data warehouse on JD Cloud, configuring network access, installing the ClickHouse client on macOS, and demonstrates both TCP and HTTP Rust integrations with example code, while noting common dependency conflicts.
Create Cluster
Log in to the JD Cloud console (https://console.jdcloud.com/) and create a Starwift cluster using the default configuration.
Create User
After the cluster is ready, create a database user in the Starwift console.
Enable Whitelist
To allow external access, edit the default security group and set the IP whitelist to 0.0.0.0/0 for testing (in production, restrict this appropriately).
Enable Public Connection
Open public network access to simplify development and testing.
macOS ClickHouse Client Installation
Install the ClickHouse client on macOS:
curl -O 'https://builds.clickhouse.com/master/macos/clickhouse' && chmod a +x ./clickhouseRefer to the official ClickHouse documentation for other platforms.
Test Connectivity
Verify the connection using the client:
clickhouse-client --host service-terrabase-9s29mdlsb7.terrabase-9s2mdsb-hb-public.jvessel2.jdcloud.com --port 9000 \
--user sample \
--password xxxxxx clickhouse:) show databasesRust Connect to Starwift
Starwift supports both TCP and HTTP protocols. Below are example Rust projects for each.
TCP Connection
Add the following dependencies to Cargo.toml:
# clickhouse tcp
tokio = { version = "1.21.2", features = ["full"] }
clickhouse-rs = { git = "https://github.com/suharev7/clickhouse-rs", features = ["default"] }Example code:
use clickhouse_rs::Pool;
#[tokio::main]
async fn main() {
let database_url = "tcp://username:password@service-terrabase-9s29mdlsb7.terrabase-9s2mdsb-hb-public.jvessel2.jdcloud.com:9000?compression=lz4".to_string();
let pool = Pool::new(database_url);
let mut client = pool.get_handle().await.unwrap();
let sql = "show databases;";
let r = client.query(sql).fetch_all().await;
println!("result is: {:?}", r);
}HTTP Connection
Add the following dependency:
# clickhouse http
clickhouse = { git = "https://github.com/loyd/clickhouse.rs", features = ["test-util"] }Example code:
use clickhouse::Client;
use clickhouse::Row;
use serde::{Deserialize, Serialize};
#[derive(Debug, Row, Serialize, Deserialize)]
struct Database {
name: String,
}
#[tokio::main]
async fn main() {
let client = Client::default()
.with_url("https://service-terrabase-9s29mdlsb7.terrabase-9sdlb7-hb-public.jvessel2.jdcloud.com:8123")
.with_user("username")
.with_password("password");
let sql = "SHOW databases";
let r = client.query(sql).fetch_all::<Database>().await;
println!("result is: {:?}", r);
}During testing, a dependency conflict arose because clickhouse-rs and clickhouse.rs both depend on different versions of clickhouse-rs-cityhash-sys, causing compilation failures. Future posts will detail the troubleshooting steps.
Thanks to the JD Cloud database team for providing the Starwift test instance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
