通过python 模式包体验下chdb

参考使用

  • 安装依赖
pip install chdb
简单代码
from chdb.session import Session
 
db = Session('./db')
db.query("CREATE DATABASE if not exists db")
db.query("USE db")
 
db.query("""
CREATE TABLE if not exists data (id UInt32, x UInt32)
ENGINE MergeTree ORDER BY id SAMPLE BY id
AS
SELECT number+1 AS id, randUniform(1, 100) AS x
FROM numbers(10000);
""")
 
query_sql = """
SELECT
  avg(x) as "avg",
  round(quantile(0.95)(x), 2) AS p95
FROM data
SAMPLE 0.1;
"""
 
res = db.query(query_sql, "PrettyCompactNoEscapes")
print(res, end="")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 效果

chdb 简单试用_python

说明

clickhouse 官方有一个关于chdb 的介绍还是值得学习看看的,内部数据处理参考图

chdb 简单试用_参考资料_02

参考资料

 https://doc.chdb.io/#/install
 https://github.com/chdb-io/chdb
 https://clickhouse.com/blog/welcome-chdb-to-clickhouse
 https://docs.python.org/3/c-api/memoryview.html