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!("Query results:\n{}", result.data_utf8()?);
// 쿼리 통계 조회
println!("Rows read: {}", result.rows_read());
println!("Bytes read: {}", result.bytes_read());
println!("Query time: {:?}", result.elapsed());
Ok(())
}