Files
rs/examples/datastore_cloud.rs
zeekay 86788566d1 datastore: rebrand the Rust client and speak the Datastore wire protocol
Cut from the upstream v0.13.3 release tag (not HEAD) so this is a pure
rename with no behaviour change. v0.13.3 is what our consumers already
resolve to: insights/rust's Cargo.lock pins clickhouse 0.13.3 and its
manifest already declares `datastore` at 0.13.x.

The load-bearing change is the HTTP auth header rename. The Datastore
server reads only X-Datastore-User / X-Datastore-Key; the upstream names
return `401 Code: 194 Authentication failed`. Every un-renamed HTTP
client is therefore unable to authenticate at all — this is a
correctness fix, not branding.

- crate `clickhouse` -> `datastore`, `clickhouse-derive` -> `datastore-derive`;
  versions preserved exactly (0.13.3 / 0.2.0).
- derive macro emits `datastore::Row` in lockstep, so downstream
  `#[derive(Row)]` keeps resolving.
- User-Agent reports `datastore-rs/{version}`.
- test-harness env vars CLICKHOUSE_* -> DATASTORE_*; compose uses
  ghcr.io/hanzoai/datastore with /etc/datastore-server config paths.
- doc links point at docs.hanzo.ai/datastore.
- `publish = false`: the `datastore` name on crates.io belongs to an
  unrelated crate, so consume this by git tag, never by version range.

CHANGELOG is truncated to a fork notice rather than find-replaced —
rewriting upstream release history would manufacture changes Datastore
never shipped. References to upstream issues keep their numbers.
2026-07-26 11:30:32 -07:00

76 lines
2.5 KiB
Rust

use datastore::sql::Identifier;
use datastore::Client;
use datastore_derive::Row;
use serde::{Deserialize, Serialize};
use std::env;
// This example requires three environment variables with your instance credentials to be set
//
// - DATASTORE_URL (e.g., https://myservice.datastore.hanzo.ai:8443)
// - DATASTORE_USER
// - DATASTORE_PASSWORD
//
// Works with either `rustls-tls` or `native-tls` cargo features.
#[tokio::main]
async fn main() -> datastore::error::Result<()> {
let table_name = "chrs_cloud";
let client = Client::default()
.with_url(read_env_var("DATASTORE_URL"))
.with_user(read_env_var("DATASTORE_USER"))
.with_password(read_env_var("DATASTORE_PASSWORD"));
// `wait_end_of_query` is required in this case, as we want these DDLs to be executed
// on the entire Cloud cluster before we receive the response.
// See https://docs.hanzo.ai/datastore/interfaces/http/#response-buffering
client
.query("DROP TABLE IF EXISTS ?")
.bind(Identifier(table_name))
.with_option("wait_end_of_query", "1")
.execute()
.await?;
// Note that you could just use MergeTree with CH Cloud, and omit the `ON CLUSTER` clause.
// The same applies to other engines as well;
// e.g., ReplacingMergeTree will become SharedReplacingMergeTree and so on.
// See https://docs.hanzo.ai/datastore/cloud/reference/shared-merge-tree#enabling-sharedmergetree
client
.query("CREATE TABLE ? (id Int32, name String) ENGINE MergeTree ORDER BY id")
.bind(Identifier(table_name))
.with_option("wait_end_of_query", "1")
.execute()
.await?;
let mut insert = client.insert(table_name)?;
insert
.write(&Data {
id: 42,
name: "foo".into(),
})
.await?;
insert.end().await?;
let data = client
.query("SELECT ?fields FROM ?")
.bind(Identifier(table_name))
// This setting is optional; use it when you need strong consistency guarantees on the reads
// See https://docs.hanzo.ai/datastore/cloud/reference/shared-merge-tree#consistency
.with_option("select_sequential_consistency", "1")
.fetch_all::<Data>()
.await?;
println!("Stored data: {data:?}");
Ok(())
}
#[derive(Debug, Serialize, Deserialize, Row)]
struct Data {
id: u32,
name: String,
}
fn read_env_var(key: &str) -> String {
env::var(key).unwrap_or_else(|_| panic!("{key} env variable should be set"))
}