Migrating Data from MySQL to MRS Hive

MRS provides enterprise-level big data clusters on the cloud. It contains HDFS, Hive, and Spark components and is applicable to massive data analysis of enterprises.

Hive supports SQL to help users perform extraction, transformation, and loading (ETL) operations on large-scale data sets. Query on large-scale data sets takes a long time. In many scenarios, you can create Hive partitions to reduce the total amount of data to be scanned each time. This significantly improves query performance.

Hive partitions are implemented by using the HDFS subdirectory function. Each subdirectory contains the column names and values of each partition. If there are multiple partitions, many HDFS subdirectories exist. It is not easy to load external data to each partition of the Hive table without relying on tools. With CDM, you can easily load data of the external data sources (relational databases, object storage services, and file system services) to Hive partition tables.

This section describes how to migrate data from the MySQL database to the MRS Hive partition table.

Scenario

Suppose that there is a trip_data table in the MySQL database. The table stores cycling records such as the start time, end time, start sites, end sites, and rider IDs. For details about the fields in the trip_data table, see Figure 1.

**Figure 1** MySQL table fields

Figure 1 MySQL table fields

The following describes how to use CDM to import the trip_data table in the MySQL database to the MRS Hive partition table. The procedure is as follows:

  1. Creating a Hive Partition Table on MRS Hive

  2. Creating a CDM Cluster and Binding an EIP to the Cluster

  3. Creating a MySQL Link

  4. Creating a Hive Link

  5. Creating a Migration Job

Prerequisites

  • MRS is available.

  • You have obtained the IP address, port, database name, username, and password for connecting to the MySQL database. In addition, the user must have the read and write permissions on the MySQL database.

  • You have uploaded a MySQL database driver by following the instructions provided in Managing Drivers.

Creating a Hive Partition Table on MRS Hive

On MRS Hive, run the following SQL statement to create a Hive partition table named trip_data with three new fields y, ym, and ymd used as partition fields. The SQL statement is as follows:

create table trip_data(TripID int,Duration int,StartDate timestamp,StartStation varchar(64),StartTerminal int,EndDate timestamp,EndStation varchar(64),EndTerminal int,Bike int,SubscriberType varchar(32),ZipCodev varchar(10))partitioned by (y int,ym int,ymd int);

Note

The trip_data partition table has three partition fields: year, year and month, and year, month, and date of the start time of a ride. For example, if the start time of a ride is 2018/5/11 9:40, the record is saved in the trip_data/2018/201805/20180511 partition. When the records in the trip_data table are summarized, only part of the data needs to be scanned, greatly improving the performance.

Creating a CDM Cluster and Binding an EIP to the Cluster

  1. Create a CDM cluster by following the instructions in Creating a Cluster.

    The key configurations are as follows:

    • The flavor of the CDM cluster is selected based on the amount of data to be migrated. Generally, cdm.medium meets the requirements for most migration scenarios.

    • The CDM and MRS clusters must be in the same VPC, subnet, and security group.

  2. After the CDM cluster is created, on the Cluster Management page, click Bind EIP in the Operation column to bind an EIP to the cluster. The CDM cluster uses the EIP to access MySQL.

    **Figure 2** Cluster list

    Figure 2 Cluster list

    Note

    If SSL encryption is configured for the access channel of a local data source, CDM cannot connect to the data source using the EIP.

Creating a Migration Job

  1. Choose Table/File Migration > Create Job to create a data migration job. Figure 4 illustrates how to create a migration job.

    **Figure 4** Creating a job for migrating data from MySQL to Hive

    Figure 4 Creating a job for migrating data from MySQL to Hive

    Note

    Set Clear Data Before Import to Yes, so that the data in the Hive table will be cleared before data import.

  2. After the parameters are configured, click Next. The Map Field tab page is displayed. See Figure 5.

    Map the fields of the MySQL table and Hive table. The Hive table has three more fields y, ym, and ymd than the MySQL table, which are the Hive partition fields. Because the fields of the source table cannot be directly mapped to the destination table, you need to configure an expression to extract data from the StartDate field in the source table.

    **Figure 5** Hive field mapping

    Figure 5 Hive field mapping

  3. Click image1 to display the Converter List dialog box, and then choose Create Converter > Expression conversion. See Figure 6.

    The expressions for the y, ym, and ymd fields are as follows:

    DateUtils.format(DateUtils.parseDate(row[2],"yyyy-MM-dd HH:mm:ss.SSS"),"yyyy")

    DateUtils.format(DateUtils.parseDate(row[2],"yyyy-MM-dd HH:mm:ss.SSS"),"yyyyMM")

    DateUtils.format(DateUtils.parseDate(row[2],"yyyy-MM-dd HH:mm:ss.SSS"),"yyyyMMdd")

    **Figure 6** Configuring the expression

    Figure 6 Configuring the expression

    Note

    The expressions in CDM support field conversion of common character strings, dates, and values.

  4. Click Next and set task parameters. Generally, retain the default values of all parameters.

    In this step, you can configure the following optional functions:

    • Retry Upon Failure: If the job fails to be executed, you can determine whether to automatically retry. Retain the default value Never.

    • Group: Select the group to which the job belongs. The default group is DEFAULT. On the Job Management page, jobs can be displayed, started, or exported by group.

    • Schedule Execution: To configure scheduled jobs, see Scheduling Job Execution. Retain the default value No.

    • Concurrent Extractors: Enter the number of extractors to be concurrently executed. Retain the default value 1.

    • Write Dirty Data: Specify this parameter if data that fails to be processed or filtered out during job execution needs to be written to OBS for future viewing. Before writing dirty data, create an OBS link. Retain the default value No so that dirty data is not recorded.

    • Delete Job After Completion: Retain the default value Do not delete.

  5. Click Save and Run. The Job Management page is displayed, on which you can view the job execution progress and result.

  6. After the job is successfully executed, in the Operation column of the job, click Historical Record to view the job's historical execution records and read/write statistics.

    On the Historical Record page, click Log to view the job logs.