myscaledb.Client
- class myscaledb.Client(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 '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py'>, **settings)[source]
Client connection class.
Usage:
async with aiohttp.ClientSession() as s: client = Client(s, compress_response=True) nums = await client.fetch("SELECT number FROM system.numbers LIMIT 100")
- Parameters
session (aiohttp.ClientSession) – aiohttp client session. Please, use one session and one Client for all connections in your app.
url (str) – Clickhouse 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 Clickhouse 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_async() None [source]
Close the session
- async cursor(query: str, *args) AsyncGenerator[Record, None] [source]
Deprecated. Use
iterate
method instead
- async execute_async(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) – Clickhouse 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) – Clickhouse 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( "INSERT INTO {table_name} VALUES", (1, (dt.date(2018, 9, 7), None)), (2, (dt.date(2018, 9, 8), 3.14)), params={"table_name": "t"} )
- Returns
Nothing.
- async fetch_async(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 – Clickhouse query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – Clickhouse 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 fetchone(query: str, *args) Optional[Record] [source]
Deprecated. Use
fetchrow
method instead
- 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 – Clickhouse query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – Clickhouse 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 – Clickhouse query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – Clickhouse 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 get_objects_async(records: List[Record])[source]
Process each row and retrieve binary data
- async is_alive_async() bool [source]
Checks if connection is Ok.
Usage:
assert await client.is_alive()
- Returns
True if connection Ok. False instead.
- 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) – Clickhouse query string.
json (bool) – Execute query in JSONEachRow mode.
params (Optional[Dict[str, Any]]) – Params to escape inside query string.
query_id (str) – Clickhouse 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.