youngsofun / clickhouse-sqlalchemy

ClickHouse dialect for SQLAlchemy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ClickHouse SQLAlchemy

ClickHouse dialect for SQLAlchemy to ClickHouse database.

https://coveralls.io/repos/github/xzkostyan/clickhouse-sqlalchemy/badge.svg?branch=master

Documentation

Documentation is available at https://clickhouse-sqlalchemy.readthedocs.io.

Usage

Supported interfaces:

Define table

from sqlalchemy import create_engine, Column, MetaData

from clickhouse_sqlalchemy import (
    Table, make_session, get_declarative_base, types, engines
)

uri = 'clickhouse+native://localhost/default'

engine = create_engine(uri)
session = make_session(engine)
metadata = MetaData(bind=engine)

Base = get_declarative_base(metadata=metadata)

class Rate(Base):
    day = Column(types.Date, primary_key=True)
    value = Column(types.Int32)

    __table_args__ = (
        engines.Memory(),
    )

Rate.__table__.create()

Insert some data

from datetime import date, timedelta

from sqlalchemy import func

today = date.today()
rates = [
    {'day': today - timedelta(i), 'value': 200 - i}
    for i in range(100)
]

And query inserted data

session.execute(Rate.__table__.insert(), rates)

session.query(func.count(Rate.day)) \
    .filter(Rate.day > today - timedelta(20)) \
    .scalar()

License

ClickHouse SQLAlchemy is distributed under the MIT license.

About

ClickHouse dialect for SQLAlchemy

License:Other


Languages

Language:Python 100.0%