Package: aiochclient
aiochclientAn async http(s) ClickHouse client for python 3.6+ supporting type
conversion in both directions, streaming, lazy decoding on select queries, and a
fully typed interface.Table of ContentsInstallationQuick StartDocumentationType ConversionConnection Pool SettingsNotes on SpeedInstallationYou can use it with eitheraiohttporhttpxhttp connectors.To use withaiohttpinstall it with command:> pip install aiochclient[aiohttp]Oraiochclient[aiohttp-speedups]to install with extra speedups.To use withhttpxinstall it with command:> pip install aiochclient[httpx]Oraiochclient[httpx-speedups]to install with extra speedups.Installing with[*-speedups]adds the following:cChardetforaiohttpspeedupaiodnsforaiohttpspeedupciso8601for ultra-fast datetime
parsing while decoding data from ClickHouse foraiohttpandhttpx.Additionally the installation process attempts to use Cython for a speed boost
(roughly 30% faster).Quick StartConnecting to ClickHouseaiochclientneedsaiohttp.ClientSessionorhttpx.AsyncClientto connect to ClickHouse:fromaiochclientimportChClientfromaiohttpimportClientSessionasyncdefmain():asyncwithClientSession()ass:client=ChClient(s)assertawaitclient.is_alive()# returns True if connection is OkQuerying the databaseawaitclient.execute("CREATE TABLE t (a UInt8, b Tuple(Date, Nullable(Float32))) ENGINE = Memory")For INSERT queries you can pass values as*args. Values should be
iterables:awaitclient.execute("INSERT INTO t VALUES",(1,(dt.date(2018,9,7),None)),(2,(dt.date(2018,9,8),3.14)),)For fetching all rows at once use thefetchmethod:all_rows=awaitclient.fetch("SELECT * FROM t")For fetching first row from result use thefetchrowmethod:row=awaitclient.fetchrow("SELECT * FROM t WHERE a=1")assertrow[0]==1assertrow["b"]==(dt.date(2018,9,7),None)You can also usefetchvalmethod, which returns first value of the first row from query result:val=awaitclient.fetchval("SELECT b FROM t WHERE a=2")assertval==(dt.date(2018,9,8),3.14)With async iteration on the query results stream you can fetch multiple
rows without loading them all into memory at once:asyncforrowinclient.iterate("SELECT number, number*2 FROM system.numbers LIMIT 10000"):assertrow[0]*2==row[1]Usefetch/fetchrow/fetchval/iteratefor SELECT queries andexecuteor
any of last for INSERT and all another queries.Working with query resultsAll fetch queries return rows as lightweight, memory efficient objects.Before
v1.0.0rows were only returned as tuples.All rows have a full mapping interface, where you can
get fields by names or indexes: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)]DocumentationTo check out theapi docs,
visit thereadthedocs site..Type Conversionaiochclientautomatically converts types from ClickHouse to python types and
vice-versa.ClickHouse typePython typeBoolboolUInt8intUInt16intUInt32intUInt64intUInt128intUInt256intInt8intInt16intInt32intInt64intInt128intInt256intFloat32floatFloat64floatStringstrFixedStringstrEnum8strEnum16strDatedatetime.dateDateTimedatetime.datetimeDateTime64datetime.datetimeDecimaldecimal.DecimalDecimal32decimal.DecimalDecimal64decimal.DecimalDecimal128decimal.DecimalIPv4ipaddress.IPv4AddressIPv6ipaddress.IPv6AddressUUIDuuid.UUIDNothingNoneTuple(T1, T2, ...)Tuple[T1, T2, ...]Array(T)List[T]Nullable(T)NoneorTLowCardinality(T)TMap(T1, T2)Dict[T1, T2]Nested(T1, T2, ...)List[Tuple[T1, T2, ...], Tuple[T1, T2, ...]]Connection Pool Settingsaiochclientuses theaiohttp.TCPConnectorto determine pool size. By default, the pool limit is 100 open connections.Notes on SpeedIt's highly recommended usinguvloopand installingaiochclientwith
speedups for the sake of speed. Some recent benchmarks on our
machines without parallelization:180k-220k rows/sec on SELECT50k-80k rows/sec on INSERTNote: these benchmarks are system dependent