A Well-Encapsulated ClickHouse Database APIs Lib
Project description
quantchdb: A Well-Encapsulated ClickHouse Database APIs Lib
Quick Start
Install quantchdb:
pip install quantchdb==0.1.9 -i https://pypi.org/simple
An example of how to use quantchdb:
1. Import quantchdb
from quantchdb import ClickHouseDatabase
import pandas as pd
import numpy as np
2. Configure ClickHouseDatabase instance
# 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_log' 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_log=True, file_log=False)
3. Functions
# Fetch data from clickhouse database
sql = "SELECT * FROM stocks.snap ORDER BY date DESC LIMIT 5"
df = db.fetch(sql)
# Execute SQL sentence
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` UInt64 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.
# Make sure the dtypes of DataFrame is consistent with dtypes of clickhouse table, or else insert_dataframe may failed.
file_path = "Your/Data/Path/kline_1m.csv"
dtype_dict = {
'exg' : np.int,
'code' : str,
'open' : np.float32,
'close' : np.float32,
'high' : np.float32,
'low' : np.float32,
'amount' : np.float32
}
df = pd.read_csv(file_path, dtype=dtype_dict)
#Int type with NA need to deal with seperately
df['volume'] = pd.to_numeric(df['volume'], errors='coerce').astype('UInt64')
db.insert_dataframe(
df=df,
table_name="etf.kline_1m",
datetime_cols=['date','date_time'],
convert_tz=False
)
# Create table from DataFrame and insert data into table automatically. This method is not recommanded, because data type inferred may be not suitable or even the sentence failed.
# You can use dtypes to make sure some columns have corrected dtypes and use other params to control the create sql sentence, though dtypes/engine/orderby/other have default values.
db.create_table_from_df(df=df,
table_name='test.etf_kline_1m',
dtypes={'code': 'String',
'date':'Date',
'date_time' :'DateTime'},
engine='ReplacingMergeTree()',
orderby='(code,date_time)',
other='PARTITION BY toYYYYMM(code)')
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.9.tar.gz
(3.9 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.9.tar.gz.
File metadata
- Download URL: quantchdb-0.1.9.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b080b7f39dbc8345fd2279c2b3352f13e5442777bc38cf1995cae9ba684fcb4f
|
|
| MD5 |
ba6a88048c0d3b5bb4ef2b6441a411ea
|
|
| BLAKE2b-256 |
1f622266d8b69c4ad3881f878ce41bf51a930da3eab7dd8ae1602d16c3c6642b
|
File details
Details for the file quantchdb-0.1.9-py3-none-any.whl.
File metadata
- Download URL: quantchdb-0.1.9-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
342e8c017a53a8a12e1a651390797ab9fcc2f595ccf1bbe3cc66f004598c4b25
|
|
| MD5 |
c75ff6fbe266047234a3fc5c609c608a
|
|
| BLAKE2b-256 |
2b23c13d787db10b0f131961b8293290764bbc1e366cc5017b15bba0f5c1c83b
|