async def query(request, ch_host, ch_port): query_id = request.query.get('query_id', None) if not query_id: raise web.HTTPBadRequest(text='Expected query id') client = Client(ch_host, port=ch_port) await client.execute('SET allow_introspection_functions=1;') try: rows = await client.execute( '''SELECT arrayStringConcat(arrayReverse(arrayMap(x -> demangle(addressToSymbol(x)), trace)), ';') AS stack, count() AS samples FROM system.trace_log WHERE query_id = %(query_id)s GROUP BY trace''', {'query_id': query_id}, ) except: raise web.HTTPBadRequest(text='Invalid query id') if len(rows) == 0: raise web.HTTPBadRequest(text='Invalid query id') result = '\n'.join([f'{row[0]} {row[1]}' for row in rows]) return web.Response(text=result)
def test_from_url(self): client = Client.from_url(f'clickhouse://{self.host}', loop=self.loop) async def run(): rv = await client.execute('SELECT 2') self.assertEqual(rv, [(2, )]) self.loop.run_until_complete(run()) self.loop.run_until_complete(client.disconnect())
async def bench_selects_aioch_with_decoding(*, retries: int, rows: int): print("AIOCH selects with decoding") client = Client(host='localhost') # prepare environment await prepare_db(client) await client.execute("INSERT INTO benchmark_tbl VALUES", list(row_data() for _ in range(rows))) # actual testing start_time = time.time() for _ in range(retries): selected_rows = await client.execute("SELECT * FROM benchmark_tbl") selected_rows = [row[0] for row in selected_rows] total_time = time.time() - start_time avg_time = total_time / retries speed = int(1 / avg_time * rows) print( f"- Avg time for selecting {rows} rows from {retries} runs: {avg_time} sec. Total: {total_time}" ) print(f" Speed: {speed} rows/sec")
def __init__(self, db_name='default', db_host='127.0.0.1', db_port='9000', username='******', password='', timeout=60): self.db_name = db_name self.db_host = db_host self.readonly = False self.timeout = timeout self.client = Client(host=db_host, port=db_port, database=db_name, user=username, password=password) self.settings = {} self.db_exists = False self.server_timezone = pytz.utc
def async_execute(query, args=None): return def sync_execute(query, args=None): return def cache_sync_execute(query, args=None, redis_client=None, ttl=None): return else: if not TEST and CLICKHOUSE_ASYNC: ch_client = Client( host=CLICKHOUSE_HOST, database=CLICKHOUSE_DATABASE, secure=CLICKHOUSE_SECURE, password=CLICKHOUSE_PASSWORD, ca_certs=CLICKHOUSE_CA, verify=CLICKHOUSE_VERIFY, ) @async_to_sync async def async_execute(query, args=None): loop = asyncio.get_event_loop() task = loop.create_task(ch_client.execute(query, args)) return task else: # if this is a test use the sync client ch_client = SyncClient( host=CLICKHOUSE_HOST, database=CLICKHOUSE_DATABASE,
def create_pool(): for i in range(inserter_config.WORKERS): client = Client(inserter_config.HOST) pool[i] = {"c": client, "a": True}
def create_client(self): return Client(self.host, self.port, self.database, 'wrong_user', loop=self.loop)