use chdb_rust::{
session::SessionBuilder,
arg::Arg,
format::OutputFormat,
log_level::LogLevel
};
use tempdir::TempDir;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 为数据库存储创建临时目录
let tmp = TempDir::new("chdb-rust")?;
// 构建配置会话
let session = SessionBuilder::new()
.with_data_path(tmp.path())
.with_arg(Arg::LogLevel(LogLevel::Debug))
.with_auto_cleanup(true) // 销毁时自动清理
.build()?;
// 创建数据库和表
session.execute(
"CREATE DATABASE demo; USE demo",
Some(&[Arg::MultiQuery])
)?;
session.execute(
"CREATE TABLE logs (id UInt64, msg String) ENGINE = MergeTree() ORDER BY id",
None,
)?;
// 插入数据
session.execute(
"INSERT INTO logs (id, msg) VALUES (1, 'Hello'), (2, 'World')",
None,
)?;
// 查询数据
let result = session.execute(
"SELECT * FROM logs ORDER BY id",
Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)]),
)?;
println!("查询结果:\n{}", result.data_utf8()?);
// 获取查询统计信息
println!("读取行数:{}", result.rows_read());
println!("读取字节数:{}", result.bytes_read());
println!("查询耗时:{:?}", result.elapsed());
Ok(())
}