A Well-Encapsulated ClickHouse Database APIs Lib
Project description
chdb: A Well-Encapsulated ClickHouse Database APIs Lib
Quick Start
An example of how to use chdb:
from chdb import ClickHouseDatabase
# To connect your clickhouse database, you need to setup your config, in which the '.env' method is recommmended for security
config = {
"host": os.getenv("DB_HOST", "localhost"),
"port": int(os.getenv("DB_PORT", 9000)),
"user": os.getenv("DB_USER", "default"),
"password": os.getenv("DB_PASSWORD", ""),
"database": os.getenv("DB_DATABASE", "default")
}
# 'terminal' and 'file_log' control the log records. 'True' denotes the corresponding log method will be executed. You can control the logs' file path by the 'log_file' param.
db = ClickHouseDatabase(config=config, terminal=True, file_log=False)
# Fetch data from clickhouse database
sql = "SELECT * FROM stocks.snap ORDER BY date DESC LIMIT 5"
df = db.fetch(sql)
# Execute sql commands
sql = f"""
CREATE TABLE IF NOT EXISTS etf.kline_1m(
`exg` UInt8 NOT NULL COMMENT '交易所标识,沪市为1,深市为0, 北交所为2',
`code` String NOT NULL COMMENT '股票代码',
`date` Date NOT NULL COMMENT '日期',
`date_time` DateTime('Asia/Shanghai') NOT NULL COMMENT '日期时间,最高精度为秒',
`time_int` UInt32 NOT NULL COMMENT '从当日开始至当前时刻的毫秒数',
`open` Float32 NULL COMMENT 'K线开始价格',
`high` Float32 NULL COMMENT 'K线内最高价',
`low` Float32 NULL COMMENT 'K线内最低价',
`close` Float32 NULL COMMENT 'K线结束价格',
`volume` Float32 NULL COMMENT 'K线内成交量',
`amount` Float32 NULL COMMENT 'K线内成交额'
)Engine = ReplacingMergeTree()
ORDER BY (code, date_time);
"""
db.execute(sql)
# Insert dataframe into clickhouse database. Before you insert your dataframe, you need to make sure the corresponding database and table are existed.
db.insert_dataframe(
df=df,
table_name="etf.kline_1m",
columns=["exg", "code", "date_time", "date", "time_int", "open", "high", "low", "close", "volume", "amount"]
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
quantchdb-0.1.1.tar.gz
(6.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quantchdb-0.1.1.tar.gz.
File metadata
- Download URL: quantchdb-0.1.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0cc66d88e56e8f1ddbe88d2bf371dbd4f00c5701a257a5b8f68e58df4315347
|
|
| MD5 |
17621c4fff06d11821c46a725120258c
|
|
| BLAKE2b-256 |
f4c947bbb71d2aafd510a4f30f4d20a7c538700d312f78bc14e3e0ac43953be2
|
File details
Details for the file quantchdb-0.1.1-py3-none-any.whl.
File metadata
- Download URL: quantchdb-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d0098d9f8ac1516f542c2565b45746a6dd0a1cef5fa11aea1e4024752c1a7ad
|
|
| MD5 |
102bb08e6f7778d6333ee30849496a85
|
|
| BLAKE2b-256 |
951d983a9353f13cc22c30aa9c2da70978bde2f43a46d2b0d6c4723412a43331
|