Connecting to a ClickHouse® database
Managed Service for ClickHouse® enables deploying and maintaining ClickHouse®
Getting started
- Create a new Managed Service for ClickHouse® cluster with public host access. You can also use an existing cluster with publicly accessible hosts.
- Configure cluster security groups.
-
Open the DataSphere project:
-
Select the project in your community or on the DataSphere home page
in the Recent projects tab. - Click Open project in JupyterLab and wait for the loading to complete.
- Open the notebook tab.
-
Connecting to a host
To connect to Managed Service for ClickHouse® cluster hosts:
-
Get an SSL certificate. To do this, enter this command in a notebook cell:
#!:bash mkdir ~/.clickhouse-client wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" -O ~/.clickhouse-client/root.crt && \ chmod 0600 ~/.clickhouse-client/root.crt -
Establish a database connection. To do this, enter this command in a notebook cell:
Using the `requests` library:Using the `clickhouse-driver` library:import requests url = 'https://{host}:8443/?database={db}&query={query}'.format( host='<ClickHouse®_host_FQDN>', db='<DB_name>', query='SELECT version()') auth = { 'X-ClickHouse-User': '<DB_username>', 'X-ClickHouse-Key': '<DB_user_password>', } cacert = '/home/jupyter/.clickhouse-client/root.crt' rs = requests.get(url, headers=auth, verify=cacert) rs.raise_for_status() print(rs.text)If you succeed connecting to the cluster, you will get the ClickHouse® version in response to your test request:
22.3.17.13%pip install clickhouse-driver from clickhouse_driver import Client client = Client(host='<ClickHouse®_host_FQDN>', user='<DB_username>', password='<DB_user_password>', database='<DB_name>', secure=True) client.execute('SELECT version()')If you succeed connecting to the cluster, you will get the ClickHouse® version in response to your test request:
[('22.3.17.13',)]
ClickHouse® is a registered trademark of ClickHouse, Inc