Use Managed Service for ClickHouse® API

To get started with the service using DoubleCloud API:

Before you start

Your primary tool to interact with the DoubleCloud is the console. We need to set it up and then configure it before moving on.

  1. Go to the console.

  2. Log in to DoubleCloud if you already have an account, or create one if you are opening the console for the first time.

    Note

    This tutorial shows how to connect to a cluster with a native CLI client and Docker , but you can use other tools of your choice. Refer to the following article to see other connection options: Connect to a ClickHouse® cluster.

  3. Install the ClickHouse® client.

  4. Create a service account:

    A service account is a specific kind of account the allows you to use a third-party service or application to interact with DoubleCloud via the API. When authenticating as a service account, such application has the access to the service determined by the service account's user role.

    1. Go to the Service accounts tab of the Members page in the console. You'll see the following dialog:

    create-service-account

    1. Name your service account.

    2. From the drop-down menu, select the Admin user role - we will need both read and write access.

    3. Click Submit. You'll see your new service account appear on the list.

  5. Issue an API key for your service account:

    1. Go to the Service accounts tab of the Members page in the console.

    2. Open the information page of the service account for which you want to create an API key.

    3. Under API keys, click Create key to create you account's first Secret key. You'll see the following dialog:

      isecret-key-dialog

    4. Click Download file with keys. You'll use it to authenticate API requests.

  6. Install the DoubleCloud API Python SDK.

Create your cluster

ClickHouse® clusters are one or more database hosts.

To create a ClickHouse® cluster, use the ClusterService create method. The required parameters to create a functional cluster:

  • project_id - the ID of your project. You can get this value on your project's information page.

  • cloud_type - aws.

  • region_id - for this quickstart, specify eu-central-1.

  • name - your cluster's name, tutorial-cluster.

  • resources - specify the following from the doublecloud.clickhouse.v1.ClusterResources model:

    • resource_preset_id - for this tutorial, go with s2-c2-m4.

    • disk_size - 34359738368 (32 GB).

    • replica_count - specify the number of replicas, 1.

    • shard_count - specify the number of shards, 1.

import json
import logging

from google.protobuf.wrappers_pb2 import Int64Value

import doublecloud
from doublecloud.clickhouse.v1.cluster_pb2 import ClusterResources
from doublecloud.clickhouse.v1.cluster_service_pb2 import CreateClusterRequest
from doublecloud.clickhouse.v1.cluster_service_pb2_grpc import ClusterServiceStub


def create_cluster(sdk, project_id, region_id, name, network_id):
    cluster_service = sdk.client(ClusterServiceStub)
    operation = cluster_service.Create(
        CreateClusterRequest(
        project_id=project_id,
        cloud_type="aws",
        region_id="eu-central-1",
        name="tutorial-cluster",
            resources=ClusterResources(
                clickhouse=ClusterResources.Clickhouse(
                    resource_preset_id="s2-c2-m4",
                    disk_size=Int64Value(value=34359738368),
                    replica_count=Int64Value(value=1),
                    shard_count=Int64Value(value=1),
                )
            ),
            network_id=network_id,
        )
    )
    logging.info("Creating initiated")
    return operation

Connect to the cluster and create a database

  1. Select Clusters from the list of services on the left.

  2. Select the name of your cluster to open its information page. By default, you will see the Overview tab.

  3. Under Connection strings, find the Native interface string and click Copy.

  4. Open your terminal and run a command to connect to your cluster:

    docker run --network host --rm -it clickhouse/<Native interface connection string>
    
    The complete Docker command structure
    docker run --network host --rm -it \ 
                clickhouse/clickhouse-client \
                --host <FQDN of your cluster> \
                --secure \
                --user <cluster user name> \
                --password <cluster user password> \
                --port 9440 
    
    <Native interface connection string>
    
  5. After you have connected to the cluster, run the CREATE query:

    CREATE DATABASE IF NOT EXISTS first_database
    

    After that, you can check the result by executing the following command:

    SHOW DATABASES
    

    The output will contain the name of your recently created database:

    ┌─name───────────────┐
    │ INFORMATION_SCHEMA │
    │ _system            │
    │ default            │
    │ first_database     │
    │ information_schema │
    │ system             │
    └────────────────────┘
    
  6. Now, it's time to add a table to the first_database on your cluster. The table will have the following structure adjusted to the dataset that we'll upload later:

    CREATE TABLE first_database.hits ON CLUSTER default (
       Hit_ID Int32, 
       Date Date, 
       Time_Spent Float32, 
       Cookie_Enabled Int32, 
       Region_ID Int32, 
       Gender String, 
       Browser String, 
       Traffic_Source String, 
       Technology String
    ) 
    ENGINE = ReplicatedMergeTree
    ORDER BY (Hit_ID, Date)
    

    Check if your query was successful and the table exists in the database:

    SHOW TABLES FROM first_database
    

    The readout will be the following:

    ┌─name─┐
    │ hits │
    └──────┘
    

Tip

Alternatively, you can use a GUI-based IDE of your choice to connect to a cluster and create a database.

Upload data

  1. Now you can open another terminal instance or exit from your cluster by typing the exit command.

  2. Run the following query to fetch the data from our S3 bucket and combine it with the INSERT query:

    curl https://doublecloud-docs.s3.eu-central-1.amazonaws.com/data-sets/hits_sample.csv
    | docker run --network host --rm -it 
    clickhouse/<Native interface connection string>
    --query="INSERT INTO first_database.hits FORMAT CSVWithNames"
    --format_csv_delimiter=";"
    
    The complete Docker command structure
    curl https://doublecloud-docs.s3.eu-central-1.amazonaws.com/data-sets/hits_sample.csv
    | docker run --network host --rm -i 
       clickhouse/clickhouse-client 
       --host <FQDN of your cluster> 
       --port 9440 --secure 
       --user <your cluster username> 
       --password <your cluster password> 
       --query="INSERT INTO first_database.hits FORMAT CSVWithNames"
       --format_csv_delimiter=";"
    
  3. If the query was successful, perform the SELECT query to see the loaded data:

    docker run --network host --rm -it 
    clickhouse/<Native interface connection string>
    --query="SELECT * FROM first_database.hits LIMIT 20"
    
    The complete Docker command structure
    docker run --network host --rm -i 
          clickhouse/clickhouse-client 
          --host <FQDN of your cluster> 
          --port 9440 --secure 
          --user <your cluster username> 
          --password <your cluster password> 
          --query="SELECT * FROM first_database.hits LIMIT 20"
    
  1. Run the following query that will fetch the data from our S3 bucket and combine it with the INSERT query:

    <Native interface connection string>
    --query="INSERT INTO first_database.hits FORMAT CSVWithNames"
    
  2. If the query was successful, perform the SELECT query to see the loaded data:

    <Native interface connection string>
    --query="SELECT * FROM first_database.hits LIMIT 20"
    

You should see your data in the table:

67112 2016-01-10 388.93976 0 184 Male   Firefox Direct          PC (Mac) 
65411 2016-01-30 267.13525 0 229 Female Firefox Search engine   PC (Windows) 
57240 2016-02-05 321.99017 0 229 Female Firefox Search engine   PC (Windows) 
34703 2016-02-11 300.81143 1 34  Male   Safari  Recommendations Tablet (Android) 
55134 2016-02-17 324.48718 0 2   Male   Firefox Direct          Other 
46247 2016-02-23 307.71362 1 2   Male   Safari  Recommendations Smartphone (Android) 
15422 2016-02-29 118.30818 1 2   Female Safari  Recommendations Smartphone (Android) 
36341 2016-03-06 330.35165 1 51  Male   Chrome  Social network  PC (Windows) 
54562 2016-03-12 325.20392 0 51  Female Chrome  Search engine   Smartphone (I0S) 
63833 2016-03-18 316.09775 1 51  Female Chrome  Search engine   Smartphone (I0S) 
43941 2016-03-24 263.73654 1 51  Male   Firefox Search engine   PC (Windows) 
38583 2016-03-30 363.8421  0 51  Female Safari  Internal        Smartphone (Android) 
65839 2016-04-05 284.4762  1 51  Male   Firefox Direct          PC (Windows) 
38024 2016-04-11 305.58286 1 51  Male   Edge    Search engine   PC (Windows) 
30573 2016-04-17 170.59955 0 52  Male   Edge    Search engine   PC (Windows) 
59083 2016-04-23 512.53    0 52  Male   Edge    Search engine   PC (Windows) 
40305 2016-04-29 324.144   0 52  Female Firefox Direct          Other 

Now you have a Managed ClickHouse® cluster with data in it. See the links below to continue exploring!

Download the complete code example

You can download the full code listing for all the steps above from our Python SDK GitHub repository .

See also