mirror of
https://github.com/hanzo-ds/rs.git
synced 2026-10-30 20:49:56 +00:00
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.
79 lines
2.3 KiB
Rust
79 lines
2.3 KiB
Rust
use std::time::{Duration, UNIX_EPOCH};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use datastore::sql::Identifier;
|
|
use datastore::{error::Result, Client, Row};
|
|
|
|
// This example demonstrates how to use asynchronous inserts, avoiding client side batching of the incoming data.
|
|
// Suitable for Hanzo Datastore Cloud, too. See https://docs.hanzo.ai/datastore/optimize/asynchronous-inserts
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Row)]
|
|
struct Event {
|
|
timestamp: u64,
|
|
message: String,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
let table_name = "chrs_async_insert";
|
|
|
|
let client = Client::default()
|
|
.with_url("http://localhost:8123")
|
|
// https://docs.hanzo.ai/datastore/operations/settings/settings#async-insert
|
|
.with_option("async_insert", "1")
|
|
// https://docs.hanzo.ai/datastore/operations/settings/settings#wait-for-async-insert
|
|
.with_option("wait_for_async_insert", "0");
|
|
|
|
client
|
|
.query(
|
|
"
|
|
CREATE OR REPLACE TABLE ? (
|
|
timestamp DateTime64(9),
|
|
message String
|
|
)
|
|
ENGINE = MergeTree
|
|
ORDER BY timestamp
|
|
",
|
|
)
|
|
.bind(Identifier(table_name))
|
|
.execute()
|
|
.await?;
|
|
|
|
let mut insert = client.insert(table_name)?;
|
|
insert
|
|
.write(&Event {
|
|
timestamp: now(),
|
|
message: "one".into(),
|
|
})
|
|
.await?;
|
|
insert.end().await?;
|
|
|
|
loop {
|
|
let events = client
|
|
.query("SELECT ?fields FROM ?")
|
|
.bind(Identifier(table_name))
|
|
.fetch_all::<Event>()
|
|
.await?;
|
|
if !events.is_empty() {
|
|
println!("Async insert was flushed");
|
|
println!("{events:?}");
|
|
break;
|
|
}
|
|
// If you change the `wait_for_async_insert` setting to 1, this line will never be printed;
|
|
// however, without waiting, you will see it in the console output several times,
|
|
// as the data will remain in the server buffer for a bit before the flush happens
|
|
println!("Waiting for async insert flush...");
|
|
tokio::time::sleep(Duration::from_millis(10)).await
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn now() -> u64 {
|
|
UNIX_EPOCH
|
|
.elapsed()
|
|
.expect("invalid system time")
|
|
.as_nanos() as u64
|
|
}
|