代码拉取完成,页面将自动刷新
A typed client for ClickHouse.
serde
for encoding/decoding rows.RowBinary
encoding.INSERT
s.schema://user:password@host[:port]/database?p=v
).RowBinaryWithNamesAndTypes
to support type conversions and better errors.To use the crate, add this to your Cargo.toml
:
[dependencies]
clickhouse = "0.10"
[dev-dependencies]
clickhouse = { version = "0.10", features = ["test-util"] }
See more examples.
Client
use clickhouse::Client;
let client = Client::default()
.with_url("http://localhost:8123")
.with_user("name")
.with_password("123")
.with_database("test");
use serde::Deserialize;
use clickhouse::Row;
#[derive(Row, Deserialize)]
struct MyRow<'a> {
no: u32,
name: &'a str,
}
let mut cursor = client
.query("SELECT ?fields FROM some WHERE no BETWEEN ? AND ?")
.bind(500)
.bind(504)
.fetch::<MyRow<'_>>()?;
while let Some(row) = cursor.next().await? { .. }
?fields
is replaced with no, name
(fields of Row
).?
is replaced with values in following bind()
calls.fetch_one::<Row>()
and fetch_all::<Row>()
can be used to get a first row or all rows correspondingly.sql::Identifier
can be used to bind table names.Nested
types.let mut insert = client.insert("some")?;
insert.write(&Row { no: 0, name: "foo" }).await?;
insert.write(&Row { no: 1, name: "bar" }).await?;
insert.end().await?;
end()
isn't called the insertion will be aborted.max_insert_block_size
.let mut inserter = client.inserter("some")?
.with_max_entries(150_000) // `250_000` by default
.with_max_duration(Duration::from_secs(15)); // `10s` by default
inserter.write(&Row { no: 0, name: "foo" }).await?;
inserter.write(&Row { no: 1, name: "bar" }).await?;
let stats = inserter.commit().await?;
if stats.entries > 0 {
println!("{} entries ({} transactions) have been inserted",
stats.entries, stats.transactions);
}
Inserter
ends an active insert in commit()
if thresholds (max_entries
, max_duration
) are reached.max_duration
) to avoid load spikes by parallel inserters.commit()
calls are inserted in the same INSERT
statement.inserter.end().await?;
client.query("DROP TABLE IF EXISTS some").execute().await?;
Requires the watch
feature.
let mut cursor = client
.watch("SELECT max(no), argMax(name, no) FROM some")
.fetch::<Row<'_>>()?;
let (version, row) = cursor.next().await?.unwrap();
println!("live view updated: version={}, row={:?}", version, row);
// Use `only_events()` to iterate over versions only.
let mut cursor = client.watch("some_live_view").limit(20).only_events().fetch()?;
println!("live view updated: version={:?}", cursor.next().await?);
lv_{sha1(query)}
to reuse the same live view by parallel watchers.JSONEachRowWithProgress
under the hood because of the issue.fetch::<u64>()
and other without specified names.The crate provides utils for mocking CH server and testing DDL, SELECT, INSERT and WATCH queries.
The functionality can be enabled with the test-util
feature. Use it only in dev-dependencies.
See the example.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。