[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for ClickHouse®](../index.md) > [Tutorials](index.md) > Copying data from Managed Service for OpenSearch to Managed Service for ClickHouse® using Yandex Data Transfer

# Copying data from Managed Service for OpenSearch to Managed Service for ClickHouse® using Yandex Data Transfer

# Copying data from Managed Service for OpenSearch to Managed Service for ClickHouse® using Yandex Data Transfer

Data Transfer enables you to transfer data from a Managed Service for OpenSearch source cluster to Managed Service for ClickHouse®.

To transfer data:

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

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


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

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).

### Required paid resources {#paid-resources}

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


## Set up your infrastructure {#prepare-infrastructure}

{% list tabs group=instructions %}

- Manually {#manual}

    {% 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. [Create a Managed Service for OpenSearch source cluster](../../managed-opensearch/operations/cluster-create.md#create-cluster) using any suitable configuration with publicly accessible hosts.
    1. In the same [availability zone](../../overview/concepts/geo-scope.md), [create a Managed Service for ClickHouse® target cluster](../operations/cluster-create.md#create-cluster) with any suitable configuration and publicly accessible hosts.

       To connect to the cluster via Yandex WebSQL, enable **WebSQL access** in the cluster settings.

    1. [Get an SSL certificate](../../managed-opensearch/operations/connect/index.md#ssl-certificate) to connect to the Managed Service for OpenSearch cluster.

    1. Make sure the [Managed Service for OpenSearch](../../managed-opensearch/operations/connect/index.md#security-groups) and [Managed Service for ClickHouse®](../operations/connect/index.md#configuring-security-groups) cluster security groups allow connecting from the internet.

- 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 [opensearch-to-clickhouse.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-opensearch-to-clickhouse/blob/main/opensearch-to-clickhouse.tf) configuration file to the same working directory.

        This file describes:

        * [Network](../../vpc/concepts/network.md#network).
        * [Subnet](../../vpc/concepts/network.md#subnet).
        * [Security group](../../vpc/concepts/security-groups.md) and rules allowing connections to the Managed Service for OpenSearch and Managed Service for ClickHouse® clusters.
        * Managed Service for OpenSearch source cluster and its `admin` account.
        * Managed Service for ClickHouse® target cluster, its user account, and a database.
        * Target endpoint.
        * Transfer.

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

        * `source_admin_password`: Managed Service for OpenSearch cluster `admin` password.
        * `mos_version`: OpenSearch version.
        * `mch_db_name`: Managed Service for ClickHouse® cluster database name.
        * `mch_username`: Managed Service for ClickHouse® cluster user name.
        * `mch_user_password`: Managed Service for ClickHouse® cluster user password.
        * `source_endpoint_id`: Source endpoint ID.
        * `profile_name`: Name of your CLI profile.

           If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    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. In the source cluster, create a test index named `people` and define its schema:

    ```bash
    curl --cacert ~/.opensearch/root.crt \
         --user <user_name_in_source_cluster>:<user_password_in_source_cluster> \
         --header 'Content-Type: application/json' \
         --request PUT 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people' && \
    curl --cacert ~/.opensearch/root.crt \
         --user <user_name_in_source_cluster>:<user_password_in_source_cluster> \
         --header 'Content-Type: application/json' \
         --request PUT 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_mapping?pretty' \
         --data'
         {
               "properties": {
                  "name": {"type": "text"},
                  "age": {"type": "integer"}
               }
         }
         '
    ```

1. Populate the test index with data:

    ```bash
    curl --cacert ~/.opensearch/root.crt \
         --user <user_name_in_source_cluster>:<user_password_in_source_cluster> \
         --header 'Content-Type: application/json' \
         --request POST 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_doc/?pretty' \
         --data'
         {
               "name": "Alice",
               "age": "30"
         }
         ' && \
    curl --cacert ~/.opensearch/root.crt \
         --user <user_name_in_source_cluster>:<user_password_in_source_cluster> \
         --header 'Content-Type: application/json' \
         --request POST 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_doc/?pretty' \
         --data'
         {
               "name": "Robert",
               "age": "32"
         }
         '
    ```

1. Optionally, check the data in the test index:

    ```bash
    curl --cacert ~/.opensearch/root.crt \
         --user <user_name_in_target_cluster>:<user_password_in_target_cluster> \
         --header 'Content-Type: application/json' \
         --request GET 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_search?pretty'
    ```

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

1. [Create a source endpoint](../../data-transfer/operations/endpoint/index.md#create) for your [pre-configured](#before-you-begin) Managed Service for OpenSearch cluster using the following settings:

    * **Database type**: `OpenSearch`.
    * **Connection settings**:
        * **Connection type**: `Managed Service for OpenSearch cluster`.
        * **Managed Service for OpenSearch cluster**: Select your Managed Service for OpenSearch cluster from the list.
        * **User**: `admin`.
        * **Password**: `admin` password.

1. Create a target endpoint and a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a target endpoint](../../data-transfer/operations/endpoint/index.md#create):

            * **Database type**: `ClickHouse`.
            * **Endpoint parameters**:
                * **Connection type**: Select `Managed cluster`.
                * **Managed cluster**: Select the Managed Service for ClickHouse® cluster from the list.
                * **User**: Enter the Managed Service for ClickHouse® cluster user name.
                * **Password**: Enter the Managed Service for ClickHouse® cluster user password.
                * **Database**: Enter the Managed Service for ClickHouse® cluster database name.

        1. [Create a transfer](../../data-transfer/operations/transfer.md#create) of the **Snapshot** type that will use the endpoints you created.
        1. [Activate](../../data-transfer/operations/transfer.md#activate) the transfer.

    - Terraform {#tf}

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

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

        1. Make sure the Terraform configuration files are correct 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.

            The transfer will be activated automatically upon creation.

    {% endlist %}

## Test the transfer {#verify-transfer}

1. Wait for the transfer status to change to **Completed**.
1. Make sure the data from the Managed Service for OpenSearch source cluster has been transferred to the Managed Service for ClickHouse® database:

    {% list tabs group=instructions %}

    - Yandex WebSQL {#websql}

      1. [Create a connection](../../websql/operations/create-connection.md#connect-cluster) to the database in the Managed Service for ClickHouse® cluster.
      1. Make sure the database contains the `people` table populated with test data by [running the following query](../../websql/operations/query-executor.md#execute-query) via the connection you created:

         ```sql
         SELECT * FROM people;
         ```

    - CLI {#cli}

      1. [Get an SSL certificate](../operations/connect/index.md#get-ssl-cert) to connect to the Managed Service for ClickHouse® cluster.
      1. If you do not have `clickhouse-client`, [install it](../operations/connect/clients.md#clickhouse-client).
      1. [Connect to the database](../operations/connect/clients.md#clickhouse-client) in the Managed Service for ClickHouse® cluster.
      1. Check that the database contains the `people` table populated with test data:

         ```sql
         SELECT * FROM people;
         ```

    {% endlist %}

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

To reduce the consumption of resources, delete those you do not need:

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

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Delete the transfer](../../data-transfer/operations/transfer.md#delete).
        1. [Delete the target endpoint](../../data-transfer/operations/endpoint/index.md#delete).
        1. [Delete the Managed Service for OpenSearch cluster](../../managed-opensearch/operations/cluster-delete.md).
        1. [Delete the Managed Service for ClickHouse® cluster](../operations/cluster-delete.md).

            Yandex WebSQL will automatically delete the database connection in the Managed Service for ClickHouse® cluster.

        1. [Delete the subnet](../../vpc/operations/subnet-delete.md).
        1. [Delete the network](../../vpc/operations/network-delete.md).

    - 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 %}

_ClickHouse® is a registered trademark of [ClickHouse, Inc](https://clickhouse.com)._