asynch
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 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 = cursor.fetchone()
assert ret == (1,)
Use fetchmany
async def fetchall():
async with conn.cursor() as cursor:
await cursor.execute("SELECT 1")
ret = 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 = 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 = cursor.fetchone()
assert ret == (1,)
pool.close()
await pool.wait_closed()
ThanksTo
- clickhouse-driver, ClickHouse Python Driver with native interface support.
License
This project is licensed under the Apache-2.0 License.
Release files for asynch 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asynch-0.2.1.tar.gz | 50.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asynch-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 120.0 kB
Release files / asynch-0.2.1.tar.gz
| Download URL | asynch-0.2.1.tar.gz |
|---|---|
| Size | 50.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e1e4bb2cc1ddc182288e3a89bd0839dfcf1e89907b397d135236d2b6a51dbbe
|
|
BLAKE2b-256 checksum How to use checksums |
0df324bcc6d59e361940c58fffb25e9182f1c6ff969d855f68af066422dcdf5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / asynch-0.2.1-py3-none-any.whl
| Download URL | asynch-0.2.1-py3-none-any.whl |
|---|---|
| Size | 69.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
362191926aa32e1b26c676b5edd8e9b73e26a1d1f68c4960b6f51dc4a649400a
|
|
BLAKE2b-256 checksum How to use checksums |
9b1076776b002d05f43551f1d5e96b5ea4ccf21db2b49fd5b1e12080bbac2284
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|