Skip to main content

A asyncio driver for ClickHouse with native tcp protocol

Project description

asynch

pypi license workflows workflows

Introduction

asynch is an asyncio ClickHouse Python Driver with native (TCP) interface support, which reuse most of clickhouse-driver and comply with PEP249.

Install

> pip install asynch

Usage

connect

from asynch import connect

async def connect_database():
    conn = await connect(
        host: str = "127.0.0.1",
        port: int = 9000,
        database: str = "default",
        user: str = "default",
        password: str = "",
    )

create table

async def create_table():
    async with conn.cursor(cursor=DictCursor) as cursor:
        await cursor.execute('create database if not exists test')
        await cursor.execute("""
        CREATE TABLE if not exists test.asynch
            (
                `id`       Int32,
                `decimal`  Decimal(10, 2),
                `date`     Date,
                `datetime` DateTime,
                `float`    Float32,
                `uuid`     UUID,
                `string`   String,
                `ipv4`     IPv4,
                `ipv6`     IPv6

            )
            ENGINE = MergeTree
                ORDER BY id"""
        )

fetchone

async def fetchone():
    async with conn.cursor() as cursor:
        await cursor.execute("SELECT 1")
        ret = cursor.fetchone()
        assert ret == (1,)

fetchmany

async def fetchall():
    async with conn.cursor() as cursor:
        await cursor.execute("SELECT 1")
        ret = cursor.fetchall()
        assert ret == [(1,)]

Use DictCursor

async def dict_cursor():
    async with conn.cursor(cursor=DictCursor) as cursor:
        await cursor.execute("SELECT 1")
        ret = cursor.fetchall()
        assert ret == [{"1": 1}]

insert

from asynch.cursors import DictCursor

async def insert_dict():
    async with conn.cursor(cursor=DictCursor) as cursor:
        ret = await cursor.execute(
            """INSERT INTO test.asynch(id,decimal,date,datetime,float,uuid,string,ipv4,ipv6) VALUES""",
            [
                {
                    "id": 1,
                    "decimal": 1,
                    "date": "2020-08-08",
                    "datetime": "2020-08-08 00:00:00",
                    "float": 1,
                    "uuid": "59e182c4-545d-4f30-8b32-cefea2d0d5ba",
                    "string": "1",
                    "ipv4": "0.0.0.0",
                    "ipv6": "::",
                }
            ],
        )
        assert ret == 1

insert with dict

async def insert_tuple():
    async with conn.cursor(cursor=DictCursor) as cursor:
        ret = await cursor.execute(
            """INSERT INTO test.asynch(id,decimal,date,datetime,float,uuid,string,ipv4,ipv6) VALUES""",
            [
                (
                    1,
                    1,
                    "2020-08-08",
                    "2020-08-08 00:00:00",
                    1,
                    "59e182c4-545d-4f30-8b32-cefea2d0d5ba",
                    "1",
                    "0.0.0.0",
                    "::",
                )
            ],
        )
        assert ret == 1

Connection Pool

async def use_pool():
    pool = await asynch.create_pool()
    async with pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute("SELECT 1")
            ret = cursor.fetchone()
            assert ret == (1,)
    pool.close()
    await pool.wait_closed()

Notice

This project is under development and mostly forked from clickhouse-driver. And this project not use in production, so PR and issue is great welcome.

ThanksTo

License

This project is licensed under the Apache-2.0 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

asynch-0.1.2.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

asynch-0.1.2-py3-none-any.whl (64.6 kB view details)

Uploaded Python 3

File details

Details for the file asynch-0.1.2.tar.gz.

File metadata

  • Download URL: asynch-0.1.2.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for asynch-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bfd8fa84a110fbc50bc854414fdb5d059cc2670c1713359eac999a1430cba133
MD5 f8ffc9c7d0269a8b47afc9e2c8042c0e
BLAKE2b-256 acc88d8d4e74a6b5e508ebfc1530e00ef1eeb7ed1246d6abd06e01ebc626e6ba

See more details on using hashes here.

File details

Details for the file asynch-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: asynch-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 64.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for asynch-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 60dfc00ce8ef0434ed9987e1b44ed9f480541bb13ae3723a8354aa97d07b6244
MD5 284fee9a40628061557272003abc5627
BLAKE2b-256 2b8447be9e1087f57b7364befd02711ed44c5a11a9166a59177055048548efc0

See more details on using hashes here.

Supported by

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