Connecting to a ClickHouse® database
In the Yandex Cloud infrastructure, ClickHouse®
Getting started
- Create a new Managed Service for ClickHouse® cluster and enable public access to it from the host. You can also use an existing cluster with publicly available hosts.
- Configure cluster security groups.
-
Open the DataSphere project:
-
Select the relevant project in your community or on the DataSphere homepage
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 the following 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 connection to the database. To do this, enter the following command in a notebook cell:
Using the requests libraryUsing the clickhouse-driver libraryimport requests url = 'https://{host}:8443/?database={db}&query={query}'.format( host='<ClickHouse®_host_FQDN>', db='<database_name>', query='SELECT version()') auth = { 'X-ClickHouse-User': '<DB_user_name>', '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 the connection to the cluster is successful, the ClickHouse® version will be output in response to the test query:
22.3.17.13
%pip install clickhouse-driver from clickhouse_driver import Client client = Client(host='<ClickHouse®_host_FQDN>', user='<DB_user_name>', password='<DB_user_password>', database='<DB_name>', secure=True) client.execute('SELECT version()')
If the connection to the cluster is successful, the ClickHouse® version will be output in response to the test query:
[('22.3.17.13',)]
ClickHouse® is a registered trademark of ClickHouse, Inc