Skip to main content

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.10  -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


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.10.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quantchdb-0.1.10-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file quantchdb-0.1.10.tar.gz.

File metadata

  • Download URL: quantchdb-0.1.10.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

Hashes for quantchdb-0.1.10.tar.gz
Algorithm Hash digest
SHA256 6b15808cec06f462d887a4c0508714d5170c94123ee2cde721a9a6cc4ac7c12a
MD5 763b9bd378a5ff56c5c564273e75c51e
BLAKE2b-256 a6e12860f04603661b1936db5a67542410f952e4cd4081b78b3350a4adc4eac6

See more details on using hashes here.

File details

Details for the file quantchdb-0.1.10-py3-none-any.whl.

File metadata

  • Download URL: quantchdb-0.1.10-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

Hashes for quantchdb-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 7ad0816f96efd0c39e0f056a75201f54e59cddd3e79261658a52439b77fabc5e
MD5 c481fb0c8672a3c92c3ec5098cbd61c6
BLAKE2b-256 3c808f8ed22e205a20dfe91f0aa529551c611d3bf9873497c83656a2f5d58c65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page