All Projects → xzkostyan → Clickhouse Sqlalchemy

xzkostyan / Clickhouse Sqlalchemy

Licence: other
ClickHouse dialect for SQLAlchemy

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Clickhouse Sqlalchemy

Clickhouse Driver
ClickHouse Python Driver with native interface support
Stars: ✭ 562 (+238.55%)
Mutual labels:  yandex, database, clickhouse
Clickhouse Net
Yandex ClickHouse fully managed .NET client
Stars: ✭ 142 (-14.46%)
Mutual labels:  yandex, database, clickhouse
Data Driven Web Apps With Pyramid And Sqlalchemy
Demos and handouts for Talk Python's Data-Driven Web Apps with Pyramid and SQLAlchemy course
Stars: ✭ 79 (-52.41%)
Mutual labels:  sqlalchemy, database
Clickhouse Go
Golang driver for ClickHouse
Stars: ✭ 1,234 (+643.37%)
Mutual labels:  database, clickhouse
Sqla Wrapper
A friendly wrapper for SQLAlchemy
Stars: ✭ 111 (-33.13%)
Mutual labels:  sqlalchemy, database
Qb
The database toolkit for go
Stars: ✭ 524 (+215.66%)
Mutual labels:  sqlalchemy, database
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (+362.05%)
Mutual labels:  sqlalchemy, database
Oreilly reactive python for data
Resources for the O'Reilly online video "Reactive Python for Data"
Stars: ✭ 98 (-40.96%)
Mutual labels:  sqlalchemy, database
Architect
A set of tools which enhances ORMs written in Python with more features
Stars: ✭ 320 (+92.77%)
Mutual labels:  sqlalchemy, database
Backendschool2019
Приложение для практического руководства по разработке бекенд-сервисов на Python (на основе вступительного испытания в Школу бэкенд‑разработки Яндекса)
Stars: ✭ 129 (-22.29%)
Mutual labels:  yandex, sqlalchemy
Datax
DataX is an open source universal ETL tool that support Cassandra, ClickHouse, DBF, Hive, InfluxDB, Kudu, MySQL, Oracle, Presto(Trino), PostgreSQL, SQL Server
Stars: ✭ 116 (-30.12%)
Mutual labels:  database, clickhouse
Openimu
Open Source Analytics & Visualisation Software for Inertial Measurement Units
Stars: ✭ 133 (-19.88%)
Mutual labels:  sqlalchemy, database
Etlalchemy
Extract, Transform, Load: Any SQL Database in 4 lines of Code.
Stars: ✭ 460 (+177.11%)
Mutual labels:  sqlalchemy, database
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (+470.48%)
Mutual labels:  database, clickhouse
Datafuse
Datafuse is a free Cloud-Native Analytics DBMS(Inspired by ClickHouse) implemented in Rust
Stars: ✭ 327 (+96.99%)
Mutual labels:  database, clickhouse
Clickhouse.client
.NET client for ClickHouse
Stars: ✭ 85 (-48.8%)
Mutual labels:  database, clickhouse
appmetrica-logsapi-loader
A tool for automatic data loading from AppMetrica LogsAPI into (local) ClickHouse
Stars: ✭ 18 (-89.16%)
Mutual labels:  yandex, clickhouse
Clickhouse Native Jdbc
ClickHouse Native Protocol JDBC implementation
Stars: ✭ 310 (+86.75%)
Mutual labels:  database, clickhouse
Tornado Sqlalchemy
SQLAlchemy support for Tornado
Stars: ✭ 112 (-32.53%)
Mutual labels:  sqlalchemy, database
Sandman2
Automatically generate a RESTful API service for your legacy database. No code required!
Stars: ✭ 1,765 (+963.25%)
Mutual labels:  sqlalchemy, database

ClickHouse SQLAlchemy

ClickHouse dialect for SQLAlchemy to ClickHouse database <https://clickhouse.yandex/>_.

.. image:: https://img.shields.io/pypi/v/clickhouse-sqlalchemy.svg :target: https://pypi.org/project/clickhouse-sqlalchemy

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

.. image:: https://img.shields.io/pypi/l/clickhouse-sqlalchemy.svg :target: https://pypi.org/project/clickhouse-sqlalchemy

.. image:: https://img.shields.io/pypi/pyversions/clickhouse-sqlalchemy.svg :target: https://pypi.org/project/clickhouse-sqlalchemy

.. image:: https://img.shields.io/pypi/dm/clickhouse-sqlalchemy.svg :target: https://pypi.org/project/clickhouse-sqlalchemy

.. image:: https://img.shields.io/travis/xzkostyan/clickhouse-sqlalchemy.svg?branch=master :target: https://travis-ci.org/xzkostyan/clickhouse-sqlalchemy

Installation

The package can be installed using pip:

.. code-block:: bash

   pip install clickhouse-sqlalchemy

Interfaces support

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

Connection Parameters

ClickHouse SQLAlchemy uses the following syntax for the connection string:

.. code-block:: python

 'clickhouse+<driver>://<user>:<password>@<host>:<port>/<database>[?key=value..]'

Where:

  • driver is driver to use. Possible choices: http, native. http is default.
  • database is database connect to. Default is default.

Drivers options

There are several options can be specified in query string.

HTTP

  • port is port ClickHouse server is bound to. Default is 8123.
  • timeout in seconds. There is no timeout by default.
  • protocol to use. Possible choices: http, https. http is default.

Connection string to database test in default ClickHouse installation:

.. code-block:: python

     'clickhouse://default:@localhost/test'

When you are using nginx as proxy server for ClickHouse server connection string might look like:

.. code-block:: python

     'clickhouse://user:[email protected]:8124/test?protocol=https'

Where 8124 is proxy port.

If you need control over the underlying HTTP connection, pass a requests.Session <https://requests.readthedocs.io/en/master/user/advanced/#session-objects>_ instance to create_engine(), like so:

.. code-block:: python

    from sqlalchemy import create_engine
    from requests import Session

    uri = 'clickhouse://default:@localhost/test'

    engine = create_engine(uri, connect_args={'http_session': Session()})

Native

Please note that native connection is not encrypted. All data including user/password is transferred in plain text. You should use this connection over SSH or VPN (for example) while communicating over untrusted network.

Connection string to database test in default ClickHouse installation:

.. code-block:: python

     'clickhouse+native://default:@localhost/test'

All connection string parameters are proxied to clickhouse-driver. See it's parameters <https://clickhouse-driver.readthedocs.io/en/latest/api.html#clickhouse_driver.connection.Connection>_.

Features

SQLAlchemy declarative support

Both declarative and constructor-style tables support:

.. code-block:: python

    from sqlalchemy import create_engine, Column, MetaData, literal

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

    uri = 'clickhouse://default:@localhost/test'

    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)
        other_value = Column(
            types.DateTime,
            clickhouse_codec=('DoubleDelta', 'ZSTD'),
        )

        __table_args__ = (
            engines.Memory(),
        )

    another_table = Table('another_rate', metadata,
        Column('day', types.Date, primary_key=True),
        Column('value', types.Int32, server_default=literal(1)),
        engines.Memory()
    )

Tables created in declarative way have lowercase with words separated by underscores naming convention. But you can easy set you own via SQLAlchemy __tablename__ attribute.

Basic DDL support

You can emit simple DDL. Example CREATE/DROP table:

.. code-block:: python

    table = Rate.__table__
    table.create()
    another_table.create()


    another_table.drop()
    table.drop()

Basic INSERT clause support

Simple batch INSERT:

.. code-block:: python

    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)]

    # Emits single INSERT statement.
    session.execute(table.insert(), rates)

Common SQLAlchemy query method chaining

order_by, filter, limit, offset, etc. are supported:

.. code-block:: python

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

    session.query(Rate.value) \
        .order_by(Rate.day.desc()) \
        .first()

    session.query(Rate.value) \
        .order_by(Rate.day) \
        .limit(10) \
        .all()

    session.query(func.sum(Rate.value)) \
        .scalar()

Advanced INSERT clause support

INSERT FROM SELECT statement:

.. code-block:: python

    from sqlalchemy import cast

    # Labels must be present.
    select_query = session.query(
        Rate.day.label('day'),
        cast(Rate.value * 1.5, types.Int32).label('value')
    ).subquery()

    # Emits single INSERT FROM SELECT statement
    session.execute(
        another_table.insert()
        .from_select(['day', 'value'], select_query)
    )

Many but not all of SQLAlchemy features are supported out of the box.

UNION ALL example:

.. code-block:: python

    from sqlalchemy import union_all

    select_rate = session.query(
        Rate.day.label('date'),
        Rate.value.label('x')
    )
    select_another_rate = session.query(
        another_table.c.day.label('date'),
        another_table.c.value.label('x')
    )

    union_all(select_rate, select_another_rate).execute().fetchone()

External data for query processing

Currently can be used with native interface.

.. code-block:: python

    ext = Table(
        'ext', metadata, Column('x', types.Int32),
        clickhouse_data=[(101, ), (103, ), (105, )], extend_existing=True
    )

    rv = session.query(Rate) \
        .filter(Rate.value.in_(session.query(ext.c.x))) \
        .execution_options(external_tables=[ext]) \
        .all()

    print(rv)

Supported ClickHouse-specific SQL

  • SELECT query:
    • WITH TOTALS
    • SAMPLE
    • lambda functions: x -> expr
    • JOIN

See tests <https://github.com/xzkostyan/clickhouse-sqlalchemy/tree/master/tests>_ for examples.

Overriding default query settings

Set lower priority to query and limit max number threads to execute the request.

.. code-block:: python

    rv = session.query(func.sum(Rate.value)) \
        .execution_options(settings={'max_threads': 2, 'priority': 10}) \
        .scalar()

    print(rv)

Running tests

.. code-block:: bash

    mkvirtualenv testenv && python setup.py test

pip will automatically install all required modules for testing.

License

ClickHouse SQLAlchemy is distributed under the MIT license <http://www.opensource.org/licenses/mit-license.php>_.

How to Contribute

#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. #. Fork the repository <https://github.com/xzkostyan/clickhouse-sqlalchemy>_ on GitHub to start making your changes to the master branch (or branch off of it). #. Write a test which shows that the bug was fixed or that the feature works as expected. #. Send a pull request and bug the maintainer until it gets merged and published.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].