# Transferring metadata between Yandex Data Processing clusters using Apache Hive™ Metastore

# Transferring metadata between Yandex Data Processing clusters using Apache Hive™ Metastore


You can transfer metadata between [Yandex Data Processing clusters](../../data-proc/concepts/index.md) with the Hive DBMS. First, you need to export metadata from a cluster, then import it into a different one using [Apache Hive™ Metastore](../concepts/metastore.md).

To transfer metadata between Yandex Data Processing clusters:

1. [Create a test table](#create-table).
1. [Export data](#export-data).
1. [Connect Yandex Data Processing to Apache Hive™ Metastore](#connect).
1. [Import data](#import-data).
1. [Check the result](#check-result).

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

{% note warning %}

If you want to configure an [access policy](../../storage/concepts/policy.md) for a bucket and connect to it from a Apache Hive™ Metastore cluster, you will need some additional infrastructure setup. For more information, see [this guide](../operations/metastore/s3-policy-connect.md).

{% endnote %}

{% note info %}

Apache Hive™ Metastore is at the [Preview](../../overview/concepts/launch-stages.md) stage.

{% endnote %}

## Required paid resources {#paid-resources}

* Yandex Data Processing cluster: use of computing resources with a Yandex Data Processing markup, use of network drives, retrieval and storage of logs, volume of outgoing traffic (see [Yandex Data Processing pricing](../../data-proc/pricing.md)).
* Apache Hive™ Metastore cluster: computing resources of cluster components (see [Yandex MetaData Hub pricing](../pricing.md)).
* Yandex Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* NAT gateway: hourly use of the gateway and its outgoing traffic (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 service account](../../iam/operations/sa/create.md) named `dataproc-s3-sa` and assign it the `dataproc.agent`, `dataproc.provisioner`, `managed-metastore.integrationProvider`, and `storage.uploader` roles.
    1. In Yandex Object Storage, [create a bucket](../../storage/operations/buckets/create.md) named `dataproc-bucket`. Grant the `READ and WRITE` permission for this bucket to the service account.
    1. [Create a cloud network](../../vpc/operations/network-create.md) named `dataproc-network`.
    1. In this network, [create a subnet](../../vpc/operations/subnet-create.md) named `dataproc-subnet`.
    1. [Set up a NAT gateway](../../vpc/operations/create-nat-gateway.md) for the subnet you created.
    1. [Create a security group](../../vpc/operations/security-group-create.md) named `dataproc-security-group` with the following rules:

        {% cut "Security group rules" %}

        #|
        || **Target service for the rule** | **Rule purpose** | **Rule settings** ||
        || Yandex Data Processing | For incoming service traffic.
        |
        * Port range: `0-65535`
        * Protocol: `Any`
        * Source: `Security group`
        * Security group: `Self` ||
        || Yandex Data Processing | For incoming traffic, to allow access to NTP servers for time syncing.
        |
        * Port range: `123`
        * Protocol: `UDP`
        * Source: `CIDR`
        * CIDR blocks: `0.0.0.0/0` ||
        || Yandex Data Processing | For incoming traffic, to connect from the internet via SSH to subcluster hosts with public access.
        |
        * Port range: `22`
        * Protocol: `TCP`
        * Source: `CIDR`
        * CIDR blocks: `0.0.0.0/0` ||
        || Apache Hive™ Metastore | For incoming client traffic.
        |
        * Port range: `30000-32767`
        * Protocol: `Any`
        * Source: `CIDR`
        * CIDR blocks: `0.0.0.0/0` ||
        || Apache Hive™ Metastore | For incoming load balancer traffic.
        |
        * Port range: `10256`
        * Protocol: `Any`
        * Source: `Load balancer health checks` ||
        || Yandex Data Processing | For outgoing service traffic.
        |
        * Port range: `0-65535`
        * Protocol: `Any`
        * Source: `Security group`
        * Security group: `Self` ||
        || Yandex Data Processing | For outgoing HTTPS traffic.
        |
        * Port range: `443`
        * Protocol: `TCP`
        * Destination: `CIDR`
        * CIDR blocks: `0.0.0.0/0` ||
        || Yandex Data Processing | For outgoing traffic, to allow access to NTP servers for time syncing.
        |
        * Port range: `123`
        * Protocol: `UDP`
        * Source: `CIDR`
        * CIDR blocks: `0.0.0.0/0` ||
        || Yandex Data Processing | For outgoing traffic, to allow Yandex Data Processing cluster connections to Apache Hive™ Metastore.
        |
        * Port range: `9083`
        * Protocol: `Any`
        * Source: `CIDR`
        * CIDR blocks: `0.0.0.0/0` ||
        |#

        {% endcut %}

    1. [Create two Yandex Data Processing clusters](../../data-proc/operations/cluster-create.md) named `dataproc-source` and `dataproc-target` with the following settings:

        * **Environment**: `PRODUCTION`.
        * **Services**:

            * `HDFS`
            * `HIVE`
            * `SPARK`
            * `YARN`
            * `ZEPPELIN`

        * **Service account**: `dataproc-s3-sa`.
        * **Availability zone**: Zone where `dataproc-subnet` resides.
        * **Properties**: `spark:spark.sql.hive.metastore.sharedPrefixes` with the `com.amazonaws,ru.yandex.cloud` value. It is required for PySpark jobs and integration with Apache Hive™ Metastore.
        * **Bucket name**: `dataproc-bucket`.
        * **Network**: `dataproc-network`.
        * **Security groups**: `dataproc-security-group`.
        * **UI Proxy**: Enabled.
        * **Subnet** for the Yandex Data Processing subclusters: `dataproc-subnet`.
        * **Public access** for the master host: Enabled.

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


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

        This file describes:

        * [Network](../../vpc/concepts/network.md#network).
        * [NAT gateway](../../vpc/concepts/gateways.md) and route table required for Yandex Data Processing.
        * [Subnet](../../vpc/concepts/network.md#subnet).
        * [Security group](../../vpc/concepts/security-groups.md) for Yandex Data Processing and Apache Hive™ Metastore.
        * [Service account](../../iam/concepts/users/service-accounts.md) for the Yandex Data Processing cluster.
        * Service account required to create an Object Storage bucket.
        * [Static access key](../../iam/concepts/authorization/access-key.md) to create a [Yandex Object Storage](../../storage/concepts/bucket.md) bucket.
        * Bucket.
        * Two Yandex Data Processing clusters.

    1. In `metastore-import.tf`, specify the following:

        * `folder_id`: Cloud folder ID, same as in the provider settings.
        * `dp_ssh_key`: Absolute path to the public key for the Yandex Data Processing clusters. Learn more about connecting to a Yandex Data Processing host over SSH [here](../../data-proc/operations/connect-ssh.md).

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

## Create a test table {#create-table}

In the `dataproc-source` cluster, create a test table named `countries`:

1. In the [management console](https://console.yandex.cloud), select a folder.
1. Navigate to **Yandex Data Processing**.
1. Open the `dataproc-source` cluster page.
1. Click the **Zeppelin Web UI** link under **UI Proxy**.
1. Select **Notebook**, then select ![image](../../_assets/console-icons/plus.svg) **Create new note**.
1. In the window that opens, specify the name for the note and click **Create**.
1. To run a PySpark job, paste a Python script into the input line:

    ```python
    %pyspark

    from pyspark.sql.types import *

    schema = StructType([StructField('Name', StringType(), True),
    StructField('Capital', StringType(), True),
    StructField('Area', IntegerType(), True),
    StructField('Population', IntegerType(), True)])

    df = spark.createDataFrame([('Australia', 'Canberra', 7686850, 19731984), ('Austria', 'Vienna', 83855, 7700000)], schema)
    df.write.mode("overwrite").option("path","s3a://dataproc-bucket/countries").saveAsTable("countries")
    ```

1. Click ![image](../../_assets/console-icons/play.svg) **Run all paragraphs** and wait until the job is complete.
1. Replace the Python code in the input line with this SQL query:

    ```sql
    %sql

    SELECT * FROM countries;
    ```

1. Click ![image](../../_assets/console-icons/play.svg) **Run all paragraphs**.

    Result:

    ```text
    |   Name    |  Capital |  Area   | Population |
    | --------- | -------- | ------- | ---------- |
    | Australia | Canberra | 7686850 | 19731984   |
    | Austria   | Vienna   | 83855   | 7700000    |
    ```

## Export data {#export-data}

To transfer data from one Yandex Data Processing cluster to another, back up the data in the `dataproc-source` cluster using `pg_dump`:

1. [Use SSH to connect](../../data-proc/operations/connect-ssh.md) to the `dataproc-source` cluster's master host:

    ```bash
    ssh ubuntu@<master_host_FQDN>
    ```

    [Learn how to get the FQDN](../../data-proc/operations/fqdn.md).

1. Create a backup and save it to the `metastore_dump.sql` file:

    ```bash
    pg_dump --data-only --schema public postgres://hive:hive-p2ssw0rd@localhost/metastore > metastore_dump.sql
    ```

1. Disconnect from the master host.
1. Download the `metastore_dump.sql` file to your local current directory:

    ```bash
    scp ubuntu@<master_host_FQDN>:metastore_dump.sql .
    ```

1. [Upload](../../storage/operations/objects/upload.md#simple) the `metastore_dump.sql` file to the `dataproc-bucket` bucket.

## Connect Yandex Data Processing to Apache Hive™ Metastore {#connect}

1. [Create a Apache Hive™ Metastore cluster](../operations/metastore/cluster-create.md) with the following parameters:

    * **Service account**: `dataproc-s3-sa`.
    * **Version**: `3.1`.
    * **Network**: `dataproc-network`.
    * **Subnet**: `dataproc-subnet`.
    * **Security groups**: `dataproc-security-group`.

1. [Add](../../data-proc/operations/cluster-update.md) to the `dataproc-target` cluster settings the `spark:spark.hive.metastore.uris` property with the following value: `thrift://<Apache Hive™ Metastore_cluster_IP_address>:9083`.

    To find out the Apache Hive™ Metastore cluster IP address, select **Yandex MetaData Hub** in the management console and then select the ![image](../../_assets/console-icons/database.svg) **Metastore** page in the left-hand panel. Copy the **IP address** column value for the cluster in question.

## Import data {#import-data}

1. Open the Apache Hive™ Metastore cluster page.
1. Click ![image](../../_assets/console-icons/arrow-down-to-square.svg) **Import**.
1. In the window that opens, specify the `dataproc-bucket` and the `metastore_dump.sql` file.
1. Click **Import**.
1. Wait for the import to complete. You can check the import status on the Apache Hive™ Metastore cluster page under ![image](../../_assets/console-icons/list-check.svg) **Operations**.

## Check the result {#check-result}

1. Open the `dataproc-target` cluster page.
1. Click the **Zeppelin Web UI** link under **UI Proxy**.
1. Select **Notebook**, then select ![image](../../_assets/console-icons/plus.svg) **Create new note**.
1. In the window that opens, specify the name for the note and click **Create**.
1. Run the following SQL query:

    ```sql
    %sql

    SELECT * FROM countries;
    ```

1. Click ![image](../../_assets/console-icons/play.svg) **Run all paragraphs**.

    Result:

    ```text
    |   Name    |  Capital |  Area   | Population |
    | --------- | -------- | ------- | ---------- |
    | Australia | Canberra | 7686850 | 19731984   |
    | Austria   | Vienna   | 83855   | 7700000    |
    ```

The metadata from the `dataproc-source` cluster was successfully imported into the `dataproc-target` cluster.

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

Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:

1. [Delete the Apache Hive™ Metastore cluster](../operations/metastore/cluster-delete.md).
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. [Yandex Data Processing clusters](../../data-proc/operations/cluster-delete.md).
        1. [Object Storage bucket](../../storage/operations/buckets/delete.md).
        1. [Route table](../../vpc/operations/delete-route-table.md).
        1. [NAT gateway](../../vpc/operations/delete-nat-gateway.md).
        1. [Security group](../../vpc/operations/security-group-delete.md).
        1. [Subnet](../../vpc/operations/subnet-delete.md).
        1. [Network](../../vpc/operations/network-delete.md).
        1. [Service account](../../iam/operations/sa/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 %}

_Apache® and [Apache Hive™](https://hive.apache.org/) are either registered trademarks or trademarks of the Apache Software Foundation in the United States and/or other countries._