• Stars
    star
    169
  • Rank 223,597 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An asyncio ClickHouse Python Driver with native (TCP) interface support.

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.

More Repositories

1

fastapi-cache

fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.
Python
1,264
star
2

fastapi-limiter

A request rate limiter for fastapi
Python
452
star
3

synch

Sync data from the other DB to ClickHouse(cluster)
Python
345
star
4

asyncmy

A fast asyncio MySQL/MariaDB driver with replication protocol support
Python
234
star
5

meilisync

Realtime sync data from MySQL/PostgreSQL/MongoDB to Meilisearch
Python
230
star
6

rearq

A distributed task queue built with asyncio and redis, with built-in web interface
Python
147
star
7

swagin

Swagger + Gin = SwaGin, a web framework based on Gin and Swagger
Go
68
star
8

trader

A framework that automated cryptocurrency exchange with strategy
Go
62
star
9

databack

Backup your data from MySQL/PostgreSQL/SSH etc. to any other storages
Python
60
star
10

longurl

A self-hosted short url service
Go
51
star
11

meilisync-admin

A web admin dashboard for meilisync
Python
31
star
12

fibers

Fiber + Swagger = Fibers, a web framework dedicated to providing a FastAPI-like development experience
Go
26
star
13

alarmer

A tool focus on error reporting for your application, like sentry but lightweight
Python
19
star
14

fastapi-rest

Fast restful API based on FastAPI and TortoiseORM
Python
10
star
15

gema-web

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
TypeScript
9
star
16

awesome-web

Search awesome projects
TypeScript
8
star
17

chcli

A Terminal Client for ClickHouse with AutoCompletion and Syntax Highlighting.
Python
6
star
18

fettler

Auto refresh cache of redis with MySQL binlog
Python
4
star
19

telsearch-web

Frontend for telsearch
TypeScript
3
star
20

mccabe

Calculate the cyclomatic complexity of the source code
Python
3
star
21

s3web

Serve static files from any S3 compatible object storage endpoints
Go
2
star
22

ClashForiOS

Swift
2
star
23

xiaoai

小爱音箱非官方SDK
Python
2
star
24

awesome

Search for awesome github project
Go
2
star
25

longurl-web

This is frontend for https://github.com/long2ice/longurl
TypeScript
2
star
26

ergo

Python
2
star
27

gema

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
Python
2
star
28

nvim

My personal neovim config
Lua
2
star
29

sponsor

Sponsor page of long2ice
HTML
2
star
30

youtube-dl-api-server

api server for youtube-dl
Python
2
star
31

talkit

A self hosted comment system
1
star
32

long2ice

My Personal README.
1
star
33

vpsmon

Python
1
star
34

homepage

My homepage
TypeScript
1
star
35

kanp

See video by download
Python
1
star
36

devme

Python
1
star
37

creatable

A tool to create table from file/database to another database
Python
1
star
38

devme-web

TypeScript
1
star
39

hugo-theme-pure

Forked from https://github.com/xiaoheiAh/hugo-theme-pure and make improvements
CSS
1
star
40

fastapi-monitor

Python
1
star
41

meilisync-web

Frontend of meilisearch-admin
Vue
1
star