Expand description
Autumn plugin for ClickHouse.
Add ClickHousePlugin to the app. Then use the ClickHouse extractor in a handler.
ClickHouse is the OLAP/analytics counterpart to the DuckDB plugin: it queries a remote
ClickHouse server over HTTP instead of an embedded database.
use autumn_plugin_clickhouse::{ClickHouse, ClickHousePlugin, ClickHouseResultExt as _};
use autumn_web::prelude::*;
#[derive(clickhouse::Row, serde::Serialize, serde::Deserialize)]
struct EventCount {
name: String,
count: u64,
}
#[get("/events/counts")]
async fn event_counts(db: ClickHouse) -> AutumnResult<Json<Vec<EventCount>>> {
let rows = db
.fetch_all::<EventCount>("SELECT name, count() AS count FROM events GROUP BY name")
.await
.or_http()?;
Ok(Json(rows))
}
autumn_web::app()
.plugin(ClickHousePlugin::new().configure(|c| {
c.url = "http://localhost:8123".into();
c.database = "analytics".into();
}))
.routes(routes![event_counts])
.run()
.await;The plugin reads [clickhouse] in autumn.toml. See config for the keys.
§What the plugin gives
ClickHouse: typedSELECTqueries with#[derive(clickhouse::Row)]structs, one-batchClickHouse::insertand the batchingInserter, plusClickHouse::executefor DDL.migrate: runs orderedCREATE TABLE IF NOT EXISTSmigrations at startup.- A
HealthOnlyreadiness check that runsSELECT 1, and Prometheus call counters.
§Limits
- Each call has the
timeout_mstimeout. A timed-out call givesClickHouseError::Timeout. - Rows must be owned structs:
#[derive(clickhouse::Row, serde::Serialize, serde::Deserialize)]without lifetimes. - Logs, error text and
Debugoutput do not have row data, server messages or passwords. - The plugin needs a reachable ClickHouse server. The startup ping stops the boot if the
server is down. Tests use the
clickhousemock server, not a live server.
Re-exports§
pub use config::ClickHouseConfig;pub use config::ConfigError;pub use config::DEFAULT_SECTION;pub use clickhouse;
Modules§
- config
- The
[clickhouse]section ofautumn.toml.
Structs§
- Click
House - The ClickHouse handle for Autumn handlers.
- Click
House Plugin - Installs a
ClickHousehandle in an Autumn app. - Insert
Stats - The statistics of an inserter commit.
- Inserter
- An insert batcher. It holds rows and commits them when a limit hits.
- Metrics
- Counters for all calls of one client.
- Migration
- One DDL statement with a name.
Enums§
- Click
House Error - An error from the plugin.
- Error
Kind - The kind of a ClickHouse error.
Constants§
- PLUGIN_
NAME - The plugin name in Autumn diagnostics.
Traits§
- Click
House Result Ext - Adds
or_httptoResult<T, ClickHouseError>.
Functions§
- migrate
- Runs the migrations in order. It stops at the first failure.