myscaledb.AsyncClient
- class myscaledb.async_db.client.AsyncClient(session=None, url: str = 'http://localhost:8123/', user: ~typing.Optional[str] = None, password: ~typing.Optional[str] = None, database: str = 'default', compress_response: bool = False, stream_batch_size: int = 1000000, json=<module 'json' from '/Users/mochix/opt/anaconda3/envs/setupTools/lib/python3.8/json/__init__.py'>, **settings)[source]
AsyncClient connection class.
Usage:
import asyncio from myscaledb import AsyncClient from aiohttp import ClientSession async def main(): async with ClientSession() as s: async with AsyncClient(s) as client: alive = await client.is_alive() print(f"Is MyScale alive? -> {alive}") res = await client.fetch(query="select id,name from default.test_table") for line in res: print(f"{line[0]}---{line[1]}") if __name__ == '__main__': asyncio.run(main())
- Parameters
session (aiohttp.ClientSession) – aiohttp client session. Please, use one session and one AsyncClient for all connections in your app.
url (str) – MyScale server url. Need full path, like http://localhost:8123/.
user (str) – User name for authorization.
password (str) – Password for authorization.
database (str) – Database name.
compress_response (bool) – Pass True if you want MyScale to compress its responses with gzip. They will be decompressed automatically. But overall it will be slightly slower.
**settings –
Any settings from https://clickhouse.yandex/docs/en/operations/settings
- async close() None [source]
Close the session
- async execute(query: str, *args, json: bool = False, params: Optional[Dict[str, Any]] = None, query_id: Optional[str] = None) None [source]
Execute query. Returns None.
- Parameters
query (str) – MyScale query string.
args – Arguments for insert queries.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – MyScale query_id.
Usage:
await client.execute( "CREATE TABLE t (a UInt8, b Tuple(Date, Nullable(Float32))) ENGINE = Memory" ) await client.execute( "INSERT INTO t VALUES", (1, (dt.date(2018, 9, 7), None)), (2, (dt.date(2018, 9, 8), 3.14)), ) await client.execute( "SELECT * FROM t WHERE a={u8}", params={"u8": 12} )
- Returns
Nothing.
- async fetch(query: str, *args, json: bool = False, params: Optional[Dict[str, Any]] = None, query_id: Optional[str] = None, decode: bool = True) List[Record] [source]
Execute query and fetch all rows from query result at once in a list.
- Parameters
query – MyScale query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – MyScale query_id.
decode – Decode to python types. If False, returns bytes for each field instead.
Usage:
all_rows = await client.fetch("SELECT * FROM t")
- Returns
All rows from query.
- async fetchrow(query: str, *args, json: bool = False, params: Optional[Dict[str, Any]] = None, query_id: Optional[str] = None, decode: bool = True) Optional[Record] [source]
Execute query and fetch first row from query result or None.
- Parameters
query – MyScale query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – MyScale query_id.
decode – Decode to python types. If False, returns bytes for each field instead.
Usage:
row = await client.fetchrow("SELECT * FROM t WHERE a=1") assert row[0] == 1 assert row["b"] == (dt.date(2018, 9, 7), None)
- Returns
First row from query or None if there no results.
- async fetchval(query: str, *args, json: bool = False, params: Optional[Dict[str, Any]] = None, query_id: Optional[str] = None, decode: bool = True) Any [source]
Execute query and fetch first value of the first row from query result or None.
- Parameters
query – MyScale query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – MyScale query_id.
decode – Decode to python types. If False, returns bytes for each field instead.
Usage:
val = await client.fetchval("SELECT b FROM t WHERE a=2") assert val == (dt.date(2018, 9, 8), 3.14)
- Returns
First value of the first row or None if there no results.
- async iterate(query: str, *args, json: bool = False, params: Optional[Dict[str, Any]] = None, query_id: Optional[str] = None, decode: bool = True) AsyncGenerator[Record, None] [source]
Async generator by all rows from query result.
- Parameters
query (str) – MyScale query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – MyScale query_id.
decode – Decode to python types. If False, returns bytes for each field instead.
Usage:
async for row in client.iterate( "SELECT number, number*2 FROM system.numbers LIMIT 10000" ): assert row[0] * 2 == row[1] async for row in client.iterate( "SELECT number, number*2 FROM system.numbers LIMIT {numbers_limit}", params={"numbers_limit": 10000} ): assert row[0] * 2 == row[1]
- Returns
Rows one by one.