Skip to main content

Simple ClickHouse SQLAlchemy Dialect

Project description

ClickHouse SQLAlchemy

ClickHouse dialect for SQLAlchemy to ClickHouse database.

https://img.shields.io/pypi/v/clickhouse-sqlalchemy.svg https://coveralls.io/repos/github/xzkostyan/clickhouse-sqlalchemy/badge.svg?branch=master https://img.shields.io/pypi/l/clickhouse-sqlalchemy.svg https://img.shields.io/pypi/pyversions/clickhouse-sqlalchemy.svg https://img.shields.io/pypi/dm/clickhouse-sqlalchemy.svg https://github.com/xzkostyan/clickhouse-sqlalchemy/actions/workflows/actions.yml/badge.svg

Documentation

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

Usage

Supported interfaces:

  • native [recommended] (TCP) via clickhouse-driver <https://github.com/mymarilyn/clickhouse-driver>

  • async native (TCP) via asynch <https://github.com/long2ice/asynch>

  • http via requests

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.

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

clickhouse-sqlalchemy-0.3.1.tar.gz (43.6 kB view details)

Uploaded Source

File details

Details for the file clickhouse-sqlalchemy-0.3.1.tar.gz.

File metadata

  • Download URL: clickhouse-sqlalchemy-0.3.1.tar.gz
  • Upload date:
  • Size: 43.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.12

File hashes

Hashes for clickhouse-sqlalchemy-0.3.1.tar.gz
Algorithm Hash digest
SHA256 e34a957739064feba561d9336fcb29a4acb68252b714ab36747158e20500b535
MD5 4713b01b26e3773cfb18707c75b88128
BLAKE2b-256 fe3d893f5d906fe781f742e28d3eee07284f4c78814c3652f9d1890863e518ca

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