[Yandex Cloud documentation](../../index.md) > [Yandex Data Transfer](../index.md) > [Tutorials](index.md) > Uploading data to data marts > Object Storage in ClickHouse®

# Loading data from Yandex Object Storage to a ClickHouse® data mart

# Loading data from Yandex Object Storage to Yandex Managed Service for ClickHouse® using Yandex Data Transfer


You can migrate data from Yandex Object Storage to a Managed Service for ClickHouse® table using Data Transfer. Proceed as follows:

1. [Prepare your test data](#prepare-data).
1. [Prepare and activate the transfer](#prepare-transfer).
1. [Test the transfer](#verify-transfer).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

* Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* Managed Service for ClickHouse® cluster: computing resources allocated to hosts, storage and backup size (see [Managed Service for ClickHouse® pricing](../../managed-clickhouse/pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Virtual Private Cloud pricing](../../vpc/pricing.md)).


## Getting started {#before-you-begin}


Set up your infrastructure:

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Create a target Managed Service for ClickHouse® cluster](../../managed-clickhouse/operations/cluster-create.md) of any suitable configuration with the following settings:

        * **DB name**: `db1`.
        * **Username**: `user1`.
        * **Password**: `<user_password>`.
        * Number of ClickHouse® hosts: Minimum of 2 to enable replication within the cluster.
        * Public access to cluster hosts: Allowed.

            {% note info %}
            
            Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.
            
            {% endnote %}

    
    1. If you are using security groups in a cluster, make sure they are [configured correctly](../../managed-clickhouse/operations/connect/index.md#configuring-security-groups) and allow connections to it.


    1. [Create an Yandex Object Storage bucket](../../storage/operations/buckets/create.md).

    
    1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `storage-viewer` with the `storage.viewer` role. The transfer will use it to access the bucket.

    1. [Create a static access key](../../iam/operations/authentication/manage-access-keys.md#create-access-key) for the `storage-viewer` service account.


- Using Terraform {#tf}

    1. If you do not have Terraform yet, [install it](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
    1. [Get the authentication credentials](../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials). You can add them to environment variables or specify them later in the provider configuration file.
    1. [Configure and initialize a provider](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). There is no need to create a provider configuration file manually, you can [download it](https://github.com/yandex-cloud-examples/yc-terraform-provider-settings/blob/main/provider.tf).
    1. Place the configuration file in a separate working directory and [specify the parameter values](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). If you did not add the authentication credentials to environment variables, specify them in the configuration file.

    1. Download the [object-storage-to-clickhouse.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-object-storage-to-clickhouse/blob/main/object-storage-to-clickhouse.tf) configuration file to your current working directory.

        This file describes:

        * [Network](../../vpc/concepts/network.md#network).
        * [Subnet](../../vpc/concepts/network.md#subnet).
        * Cluster access [security group](../../vpc/concepts/security-groups.md).
        * Service account for bucket operations, e.g., creation and access.
        * Yandex Lockbox secret for the service account static key required to configure the source endpoint.
        * Object Storage source bucket.
        * Managed Service for ClickHouse® target cluster.
        * Target endpoint.
        * Transfer.

    1. In the `object-storage-to-clickhouse.tf` file, specify the following:

        * `folder_id`: Cloud folder ID, same as in the provider settings.
        * `bucket_name`: Bucket name consistent with the [naming conventions](../../storage/concepts/bucket.md#naming).
        * `ch_password`: ClickHouse® user password.

    1. Validate your Terraform configuration files using this command:

        ```bash
        terraform validate
        ```

        Terraform will display any configuration errors detected in your files.

    1. Create the required infrastructure:

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

        All the required resources will be created in the specified folder. You can check resource availability and their settings in the [management console](https://console.yandex.cloud).

{% endlist %}

## Prepare your test data {#prepare-data}

1. Create two CSV files with test data:

    * `demo_data1.csv`:

        ```csv
        1,Anna
        2,Robert
        3,Umar
        4,Algul
        5,Viktor
        ```

    * `demo_data2.csv`:

        ```csv
        6,Maria
        7,Alex
        ```

1. [Upload](../../storage/operations/objects/upload.md#simple) the `demo_data1.csv` file to the Object Storage bucket.

## Prepare and activate a transfer {#prepare-transfer}

1. [Create a source endpoint](../operations/endpoint/source/object-storage.md#endpoint-settings) of the `Object Storage` type with the following settings:

    * **Database type**: `Object Storage`.
    * **Bucket**: Object Storage bucket name.

    
    * **Access Key ID**: Service account’s static access key ID. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).
    * **Secret Access Key**: Service account’s secret access key. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).


    * **Endpoint**: `https://storage.yandexcloud.net`.
    * **Region**: `ru-central1`.
    * **Data format**: `CSV`.
    * **Delimiter**: Comma (`,`).
    * **Table**: `table1`.
    * **Result table schema**: Select `Manual` and specify the following field names and data types:

        * `Id`: `Int64`
        * `Name`: `UTF8`

    Leave the other settings at their defaults.

1. Create a target endpoint and a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a `ClickHouse®` target endpoint](../operations/endpoint/target/clickhouse.md#endpoint-settings) with these cluster connection settings:

            * **Connection type**: `Managed cluster`.
            * **Managed cluster**: `<ClickHouse®_target_cluster_name>` from the drop-down list.
            * **Database**: `db1`.
            * **User**: `user1`.
            * **Password**: `<user_password>`.

        1. [Create a transfer](../operations/transfer.md#create) of the **_Snapshot and replication_**-type that will use the endpoints you created.

        1. [Activate the transfer](../operations/transfer.md#activate) and wait for its status to change to **Replicating**.

    - Using Terraform {#tf}

        1. In the `object-storage-to-clickhouse.tf` file, specify the following settings:

            * `source_endpoint_id`: Source endpoint ID.
            * `transfer_enabled`: Set to `1` to create a transfer.

        1. Validate your Terraform configuration files using this command:

            ```bash
            terraform validate
            ```

            Terraform will display any configuration errors detected in your files.

        1. Create the required infrastructure:

            1. Run this command to view the planned changes:
            
               ```bash
               terraform plan
               ```
            
               If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
            
            1. If everything looks correct, apply the changes:
               1. Run this command:
            
                  ```bash
                  terraform apply
                  ```
            
               1. Confirm updating the resources.
               1. Wait for the operation to complete.

        1. The transfer will be activated automatically. Wait for its status to change to **Replicating**.

    {% endlist %}

## Test the transfer {#verify-transfer}

Make sure the transfer works correctly by testing copying and replication.

### Test copying {#verify-copy}

1. [Connect](../../managed-clickhouse/operations/connect/clients.md) to the `db1` database in the Managed Service for ClickHouse® target cluster.

1. Run this query:

    ```sql
    SELECT * FROM db1.table1;
    ```

    {% cut "Response example" %}

    ```sql
      __file_name  | __row_index | Id |  Name
    --------------+-------------+----+--------
    demo_data1.csv |           1 |  1 | Anna
    demo_data1.csv |           2 |  2 | Robert
    demo_data1.csv |           3 |  3 | Umar
    demo_data1.csv |           4 |  4 | Algul
    demo_data1.csv |           5 |  5 | Viktor
    ```

    {% endcut %}

### Test replication {#verify-replication}

1. [Upload](../../storage/operations/objects/upload.md#simple) the `demo_data2.csv` file to the Object Storage bucket.

1. Make sure the data from `demo_data2.csv` has been added to the target database:

    1. [Connect](../../managed-clickhouse/operations/connect/clients.md) to the `db1` database in the Managed Service for ClickHouse® target cluster.

    1. Run this query:

        ```sql
        SELECT * FROM db1.table1;
        ```

        {% cut "Response example" %}

        ```sql
          __file_name  | __row_index | Id |  Name
        --------------+-------------+----+--------
        demo_data1.csv |           1 |  1 | Anna
        demo_data1.csv |           2 |  2 | Robert
        demo_data1.csv |           3 |  3 | Umar
        demo_data1.csv |           4 |  4 | Algul
        demo_data1.csv |           5 |  5 | Viktor
        demo_data2.csv |           1 |  6 | Maria
        demo_data2.csv |           2 |  7 | Alex
        ```

        {% endcut %}

## Delete the resources you created {#clear-out}

{% note info %}

Before deleting any resources, [deactivate the transfer](../operations/transfer.md#deactivate).

{% endnote %}

To minimize resource consumption, delete the resources you no longer need:

1. [Delete the transfer](../operations/transfer.md#delete).
1. [Delete the source endpoint](../operations/endpoint/index.md#delete).
1. [Delete the objects](../../storage/operations/objects/delete.md) from the bucket.
1. Delete the rest of the resources depending on how you created them:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Delete the target endpoint](../operations/endpoint/index.md#delete).
        1. [Delete the Managed Service for ClickHouse® cluster](../../managed-clickhouse/operations/cluster-delete.md).
        1. [Delete the Object Storage bucket](../../storage/operations/buckets/delete.md).

    - Using Terraform {#tf}

        1. In the terminal window, go to the directory containing the infrastructure plan.
        
            {% note warning %}
        
            Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
        
            {% endnote %}
        
        1. Delete resources:
        
            1. Run this command:
        
                ```bash
                terraform destroy
                ```
        
            1. Confirm deleting the resources and wait for the operation to complete.
        
            All the resources described in the Terraform manifests will be deleted.

    {% endlist %}