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

or if you want to install clickhouse-cityhash to enable transport compression

> pip install asynch[compression]

Usage

Connect to ClickHouse

from asynch import connect

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

Create table by sql

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

Use fetchone

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

Use fetchmany

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

Use DictCursor to get result with dict

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

Insert data with dict

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 data with tuple

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

Use 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 = await cursor.fetchone()
            assert ret == (1,)
    pool.close()
    await pool.wait_closed()

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_lxneng-0.2.3.tar.gz (53.3 kB view details)

Uploaded Source

Built Distribution

asynch_lxneng-0.2.3-py3-none-any.whl (73.3 kB view details)

Uploaded Python 3

File details

Details for the file asynch_lxneng-0.2.3.tar.gz.

File metadata

  • Download URL: asynch_lxneng-0.2.3.tar.gz
  • Upload date:
  • Size: 53.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/22.5.0

File hashes

Hashes for asynch_lxneng-0.2.3.tar.gz
Algorithm Hash digest
SHA256 351e2e98ef6287c3d2b4ed9fc503975fe89f5c96cab300010ba2535989f00d97
MD5 3eb57a8d541d223681c4ab884a23e056
BLAKE2b-256 1c4169c339f9feddb95a3013fa95e00facbe12416775d5e19eaa061831d8394e

See more details on using hashes here.

File details

Details for the file asynch_lxneng-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: asynch_lxneng-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 73.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/22.5.0

File hashes

Hashes for asynch_lxneng-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9c246b25ce0d9c633c996a2445d2fa3b475e27c95d45108a4c09d8d94676eb05
MD5 8618a65d6c9434e7036f3367c0c56960
BLAKE2b-256 3bc4fdfe1b888665c30ee3ed83d8a58dbcf4c05648bd224d503f0fd7de011cda

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