异步http://clickhouse client for python 3.6+
aiochclient的Python项目详细描述
aiochclient
异步http(s)clickhouse client for python 3.6+具有双向转换类型、流式支持、对选定查询的延迟解码和全类型接口
内容
安装
> pip install aiochclient
或安装时附加加速要求:
> pip install aiochclient[speedups]
它将另外安装cChardet 和aiodns加速 和ciso8601用于超快 从ClickHouse解码数据时进行DateTime分析。
同时,在安装时,它将尝试建立cython扩展以提高速度(约30%)。
快速启动
连接到clickhouse
aiochclient
连接需要aiohttp.ClientSession
:
fromaiochclientimportChClientfromaiohttpimportClientSessionasyncdefmain():asyncwithClientSession()ass:client=ChClient(s)assertawaitclient.is_alive()# returns True if connection is Ok
查询
awaitclient.execute("CREATE TABLE t (a UInt8, b Tuple(Date, Nullable(Float32))) ENGINE = Memory")
对于insert查询,可以将值作为*args
传递。值应为iterables:
awaitclient.execute("INSERT INTO t VALUES",(1,(dt.date(2018,9,7),None)),(2,(dt.date(2018,9,8),3.14)),)
要同时获取所有行,请使用fetch
方法:
all_rows=awaitclient.fetch("SELECT * FROM t")
要从结果中获取第一行,请使用fetchrow
方法:
row=awaitclient.fetchrow("SELECT * FROM t WHERE a=1")assertrow[0]==1assertrow["b"]==(dt.date(2018,9,7),None)
您还可以使用fetchval
方法,它返回
查询结果第一行的第一个值:
val=awaitclient.fetchval("SELECT b FROM t WHERE a=2")assertval==(dt.date(2018,9,8),3.14)
通过查询结果的异步迭代,您可以获取 多行而不同时将它们全部加载到内存中:
asyncforrowinclient.iterate("SELECT number, number*2 FROM system.numbers LIMIT 10000"):assertrow[0]*2==row[1]
使用fetch
/fetchrow
/fetchval
/iterate
进行选择查询
和execute
或最后一个for insert和所有其他查询。
使用查询结果
所有fetch查询都将行作为轻量级内存返回
有效对象(来自v1.0.0
,在它之前-只有元组)
具有完整的映射接口,其中
您可以按名称或索引获取字段:
row=awaitclient.fetchrow("SELECT a, b FROM t WHERE a=1")assertrow["a"]==1assertrow[0]==1assertrow[:]==(1,(dt.date(2018,9,8),3.14))assertlist(row.keys())==["a","b"]assertlist(row.values())==[1,(dt.date(2018,9,8),3.14)]
类型转换
aiochclient
自动将值转换为所需的类型
从clickhouse response和for client insert查询。
ClickHouse type | Python type |
---|---|
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | ^{ |
连接池
如果要更改连接池大小,可以使用 aiohttp.TCPConnector。 请注意,默认池限制为100个连接。
速度
使用uvloop
并与aiochclient[speedups]
一起安装
为了速度,强烈推荐使用。
至于最后一个版本的aiochclient
它的速度
使用一个任务(没有集合或并行
客户等)是关于
180k-220k行/秒关于select和about
50k-80k行/秒关于插入查询
取决于其环境和clickhouse设置。
如果这个项目对你有帮助,请启动这个存储库!