# Copying data from Managed Service for OpenSearch to Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer

# Copying data from Yandex Managed Service for OpenSearch to Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer

With Yandex Data Transfer, you can transfer data from a Yandex Managed Service for OpenSearch source cluster to a Greenplum® target cluster in Yandex MPP Analytics for PostgreSQL.

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)).
* Yandex MPP Analytics for PostgreSQL cluster: use of computing resources allocated to hosts, storage and backup size (see [Yandex MPP Analytics for PostgreSQL pricing](../../managed-greenplum/pricing/index.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 Greenplum® target cluster](../../managed-greenplum/operations/cluster-create.md#create-cluster) of any suitable configuration. When creating your cluster:
        * Enable public access for the hosts.
        * Enable **Data Transfer access**.
    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 [Greenplum®](../../managed-greenplum/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-greenplum.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-opensearch-to-greenplum/blob/main/opensearch-to-greenplum.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 OpenSearch and Greenplum® clusters.
        * Managed Service for OpenSearch source cluster and its `admin` account.
        * Greenplum® target cluster in Yandex MPP Analytics for PostgreSQL.
        * Transfer.

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

        * `mos_cluster_name`: Managed Service for OpenSearch cluster name.
        * `mos_version`: OpenSearch version.
        * `mos_admin_password`: Managed Service for OpenSearch cluster `admin` password.
        * `mgp_cluster_name`: Greenplum® cluster name.
        * `mgp_username`: Greenplum® cluster user name.
        * `mgp_user_password`: Greenplum® cluster user password.
        * `transfer_name`: Data Transfer transfer name.
        * `profile_name`: Your YC CLI profile name.

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

    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.

        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' -d'
         {
               "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' -d'
         {
               "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' -d'
         {
               "name": "Robert",
               "age": "32"
         }
         '
    ```

1. Make sure the data is saved 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](../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](../operations/endpoint/index.md#create) for the Greenplum® cluster you [created earlier](#before-you-begin), with the following settings:

    * **Database type**: `Greenplum`.
    * **Endpoint parameters**:
        * **Connection type**: Select `Cluster`.
        * **Cluster**: Select your Greenplum® cluster from the list.
        * **Database**: `postgres`.
        * **User**: Enter the Greenplum® cluster user name.
        * **Password**: Enter the Greenplum® cluster user password.

1. Create a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

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

    - Terraform {#tf}

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

            * `source_endpoint_id`: Source endpoint ID.
            * `target_endpoint_id`: Target endpoint ID.
            * `transfer_enabled`: Set to `1` to create 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 source Managed Service for OpenSearch cluster has been transferred to the Yandex MPP Analytics for PostgreSQL cluster:

   1. [Get an SSL certificate](../../managed-greenplum/operations/connect/index.md#get-ssl-cert) to connect to the Greenplum® cluster.
   1. Install the dependencies:

      ```bash
      sudo apt update && sudo apt install --yes postgresql-client
      ```

   1. Connect to the database in the Greenplum® cluster.
   1. Check that the database contains the `people` table populated with test data:

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

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

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

{% list tabs group=instructions %}

  - Manually {#manual}

      1. [Delete the Managed Service for OpenSearch cluster](../../managed-opensearch/operations/cluster-delete.md).
      1. [Delete the Greenplum® cluster](../../managed-greenplum/operations/cluster-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 %}