Connect Remote Clients
Now that we’ve shown how to create a cluster and use ClickHouse SQL queries into your new cluster, let’s connect to it remotely.
For the following, we’re going to be using the clickhouse-client program, but the same process will help you gain access from your favorite client.
Full instructions for installing ClickHouse can be found on the ClickHouse Installation page. We’ll keep this simple and assume you’re using a Linux environment like Ubuntu. For this example, we set up a virtual machine running Ubuntu 20.04.
First, we need to know our connection details for our Altinity.Cloud ClickHouse cluster. To view your connection details:
From the Clusters View, select Connection Details for the cluster to manage.
From here, you can copy and paste the settings for the ClickHouse client from your cluster’s Connection Details. For example:
clickhouse-client -h yourdataset.yourcluster.altinity.cloud --port 9440 -s --user=admin --password
The ClickHouse Client is a command line based program that will be familiar to SQL based users.
Setting Up ClickHouse Client in Linux
If you’ve already set up ClickHouse client, then you can skip this step. These instructions are modified from the Altinity Stable Builds Quick Start Guides to quickly get a ClickHouse client running on your system.
Deb Linux Based Installs
For those who need quick instructions on how to install ClickHouse client in their deb based Linux environment (like Ubuntu), use the following:
sudo apt-get update
Install the Altinity package signing keys:
sudo sh -c 'mkdir -p /usr/share/keyrings && curl -s https://builds.altinity.cloud/apt-repo/pubkey.gpg | gpg --dearmor > /usr/share/keyrings/altinity-dev-archive-keyring.gpg'
apt-getrepository to include the Altinity Stable build repository with the following commands:
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/altinity-dev-archive-keyring.gpg] https://builds.altinity.cloud/apt-repo stable main" > /etc/apt/sources.list.d/altinity-dev.list'
sudo apt-get update
Install the most current version of the Altinity Stable Build for ClickHouse client with the following:
sudo apt-get install clickhouse-client
macOS Based Installs
brew install altinity/clickhouse/clickhouse
Connect With ClickHouse Client
If your ClickHouse client is ready, then you can copy and paste your connection settings into your favorite terminal program, and you’ll be connected.
ClickHouse Python Driver
Users who prefer Python can use the clickhouse-driver to connect through Python. These instructions are very minimal, and are intended just to get you working in Python with your new Altinity.Cloud cluster.
These instructions are in the
bash shell and require the following be installed in your environment:
Python 3.7and above
The Python module venv
IMPORTANT NOTE: Install the
clickhouse-driver0.2.0 or above which has support for Server Name Indication (SNI) when using TLS communications.
To connect with the Python clickhouse-driver:
(Optional) Setup your local environment. For example:
python3 -m venv test . test/bin/activate
Install the driver with pip3:
pip3 install clickhouse-driver
Add the client and connection details. The Access Point provides the necessary information to link directly to your cluster.
Import the clickhouse_driver client and enter the connection settings:
from clickhouse_driver import Client client = Client('<HOSTNAME>', user='admin', password=<PASSWORD>, port=9440, secure='y', verify=False)
Run client.execute and submit your query. Let’s just look at the tables from within Python:
>>> client.execute('SHOW TABLES in default') [('events',), ('events_local',)]
You can perform selects and inserts as you need. For example, continuing from our Your First Queries using Cluster Explore.
>>> result = client.execute('SELECT * FROM default.events') >>> print(result) [(datetime.date(2020, 11, 23), 1, 13, 'Example')]
For more information see the article ClickHouse And Python: Getting To Know The ClickHouse-driver Client.