If you want to perform operations such as full-text searches, multi-dimensional queries, and statistical analysis on data in MySQL, you can use the Data Integration service of DataWorks to synchronize the data to Alibaba Cloud Elasticsearch. The data can be synchronized within minutes or a longer period of time. This topic describes how to use the Data Integration service to synchronize data from ApsaraDB RDS for MySQL to Alibaba Cloud Elasticsearch.
Background information
DataWorks is an end-to-end big data development and governance platform based on big data compute engines. DataWorks provides features such as data development, task scheduling, and data management. You can create synchronization tasks in DataWorks to rapidly synchronize data from various data sources to Alibaba Cloud Elasticsearch.
The following types of data sources are supported:
Alibaba Cloud databases: ApsaraDB RDS for MySQL, ApsaraDB RDS for PostgreSQL, ApsaraDB RDS for SQL Server, ApsaraDB for MongoDB, and ApsaraDB for HBase
Alibaba Cloud PolarDB for Xscale (PolarDB-X) (formerly DRDS)
Alibaba Cloud MaxCompute
Alibaba Cloud Object Storage Service (OSS)
Alibaba Cloud Tablestore
Self-managed databases: HDFS, Oracle, FTP, Db2, MySQL, PostgreSQL, SQL Server, MongoDB, and HBase
The following synchronization scenarios are supported:
Synchronize big data from a database or table to Alibaba Cloud Elasticsearch in offline mode. For more information, see Create a batch synchronization task to synchronize all data in a database to Elasticsearch.
Synchronize full and incremental big data to Alibaba Cloud Elasticsearch in real time. For more information, see Create a real-time synchronization task to synchronize data to Elasticsearch.
Prerequisites
An ApsaraDB RDS for MySQL instance is created. For more information, see Create an ApsaraDB RDS for MySQL instance. In this example, an ApsaraDB RDS instance that runs MySQL 5.7 is created.
An Alibaba Cloud Elasticsearch cluster is created, and the Auto Indexing feature is enabled for the cluster. For more information, see Create an Alibaba Cloud Elasticsearch cluster and Configure the YML file.
A DataWorks workspace is created. For more information, see Create a workspace.
You can synchronize data only to Alibaba Cloud Elasticsearch. Self-managed Elasticsearch is not supported.
The ApsaraDB RDS for MySQL instance, Elasticsearch cluster, and DataWorks workspace must reside in the same region.
The ApsaraDB RDS for MySQL instance, Elasticsearch cluster, and DataWorks workspace must be in the same time zone. Otherwise, if you synchronize time-related data, the data in the source and the data in the destination after the synchronization may have a time zone difference.
Billing
For information about the billing of Alibaba Cloud Elasticsearch clusters, see Elasticsearch billable items.
For information about the billing of exclusive resource groups for Data Integration, see Billing of exclusive resource groups for Data Integration (subscription).
Procedure
In this example, synchronization in offline mode is used. For information about how to synchronize data in real time, see Create a real-time synchronization task to synchronize data to Elasticsearch.
Step 1: Prepare source data
Create a database and a table in the ApsaraDB RDS for MySQL instance.
You can use an ApsaraDB RDS for MySQL database or use a self-managed database that is hosted on your on-premises machine. In this example, an ApsaraDB RDS for MySQL database is used. For more information, see Getting started.
In this example, the following statements are executed to create a table and insert data into the table:
-- create table CREATE TABLE `es_test` ( `id` bigint(32) NOT NULL, `name` varchar(32) NULL, `age` bigint(32) NULL, `hobby` varchar(32) NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; -- insert data INSERT INTO `es_test` (`id`,`name`,`age`,`hobby`) VALUES (1,'user1',22,'music'); INSERT INTO `es_test` (`id`,`name`,`age`,`hobby`) VALUES (2,'user2',23,'sport'); INSERT INTO `es_test` (`id`,`name`,`age`,`hobby`) VALUES (3,'user3',43,'game'); INSERT INTO `es_test` (`id`,`name`,`age`,`hobby`) VALUES (4,'user4',24,'run'); INSERT INTO `es_test` (`id`,`name`,`age`,`hobby`) VALUES (5,'user5',42,'basketball');
Step 2: Create and configure an exclusive resource group for Data Integration
Create an exclusive resource group for Data Integration, and associate the resource group with a virtual private cloud (VPC) and the created workspace. Exclusive resource groups ensure fast and stable data transmission.
Log on to the DataWorks console.
In the top navigation bar, select a region. In the left-side navigation pane, click Resource Group.
On the Exclusive Resource Groups tab of the Resource Groups page, click Create Resource Group for Data Integration of Old Version.
On the DataWorks Exclusive Resources page, set Type to Exclusive Resource Groups for Data Integration, configure Resource Group Name, and then click Buy Now. On the page that appears, click Pay.
For more information, see Create an exclusive resource group for Data Integration.
On the Exclusive Resource Groups tab, find the created resource group and click Network Settings in the Actions column to associate the resource group with a VPC.
For more information, see Associate the exclusive resource group for Data Integration with a VPC.
NoteIn this example, an exclusive resource group for Data Integration is used to synchronize data over a VPC. For more information, see Configure an IP address whitelist.
The exclusive resource group must be connected to the VPC where the ApsaraDB RDS for MySQL instance resides and the VPC where the Elasticsearch cluster resides. This way, data can be synchronized based on the exclusive resource group. Therefore, you must associate the exclusive resource group with the VPC, zone, and vSwitch of the ApsaraDB RDS for MySQL instance and with those of the Elasticsearch cluster. For information about how to view the VPC where the ApsaraDB RDS for MySQL instance resides, see Change the VPC and vSwitch. For information about how to view the VPC where the Elasticsearch cluster resides, see View the basic information of a cluster.
ImportantAfter you associate the exclusive resource group with the VPCs, you need to add the CIDR blocks of the vSwitches to which the ApsaraDB RDS for MySQL instance and the Elasticsearch cluster belong to the private IP address whitelists of the ApsaraDB RDS for MySQL instance and Elasticsearch cluster. For more information, see Configure an IP address whitelist for an ApsaraDB RDS for MySQL instance and Configure a public or private IP address whitelist for an Elasticsearch cluster.
Click the back icon in the upper-left corner of the page to return to the Resource Groups page.
On the Exclusive Resource Groups tab, find the resource group and click Associate Workspace in the Actions column to associate the resource group with the created workspace.
For more information, see Associate the exclusive resource group for Data Integration with a workspace.
Step 3: Add data sources
Add the ApsaraDB RDS for MySQL instance and Elasticsearch cluster to Data Integration as data sources.
Go to the Data Integration page.
Log on to the DataWorks console.
In the left-side navigation pane, click Workspace
Find the workspace and choose
in the Actions column.
In the left-side navigation pane of the Data Integration page, click Data source.
Add an ApsaraDB RDS for MySQL data source.
On the Data Sources page, click Add Data Source.
In the Add Data Source dialog box, search for and select MySQL.
In the Add MySQL Data Source dialog box, configure the parameters in the Basic Information section.
For more information, see Add a MySQL data source.
Find the resource group in the Connection Configuration section and click Test Network Connectivity. If Connected is displayed, the resource group is connected to the data source.
Click Complete.
Add an Elasticsearch data source in the same way. For more information, see Add an Elasticsearch data source.
Step 4: Configure and run a batch synchronization task
The exclusive resource group is used to run the batch synchronization task. The resource group obtains data from the source and writes the data to the Elasticsearch cluster.
You can use the codeless UI or code editor to configure the batch synchronization task. In this example, the codeless UI is used. For information about how to use the code editor to configure the batch synchronization task, see Configure a batch synchronization task by using the code editor, MySQL Reader, and Elasticsearch Writer.
Go to the DataStudio page of DataWorks.
Log on to the DataWorks console.
In the left-side navigation pane, click Workspace
Find the workspace and choose
in the Actions column.
Create a batch synchronization task.
In the left-side navigation pane, choose
to create a workflow.Right-click the name of the newly created workflow and choose
.In the Create Node dialog box, configure the Name parameter and click Confirm.
Configure the network and resources.
For the source part, set Source to MySQL and Data Source Name to the name of the added ApsaraDB RDS for MySQL data source.
For the resource group part, select the created exclusive resource group.
For the destination part, set Destination to Elasticsearch and Data Source Name to the name of the added Elasticsearch data source.
Click Next.
Configure the task.
In the Source section, select the table whose data you want to synchronize.
In the Destination section, configure the parameters.
In the Field Mapping section, configure mappings between source fields and destination fields.
In the Channel Control section, configure the parameters.
For more information, see Configure a batch synchronization task by using the codeless UI.
Run the task.
(Optional) Configure scheduling properties for the task. In the right-side navigation pane, click Properties. On the Properties tab, configure the parameters based on your business requirements. For more information about the parameters, see Scheduling configuration.
In the upper-left corner, click the Save icon to save the task.
In the upper-left corner, click the Submit icon to submit the task.
If you configure scheduling properties for the task, the task is automatically run on a regular basis. You can also click the Run icon in the upper-left corner to run the task immediately.
If
Shell run successfully!
is displayed in operational logs, the task runs successfully.
Step 5: Verify the data synchronization result
Log on to the Kibana console of the Elasticsearch cluster. For more information, see Log on to the Kibana console.
Click the icon in the upper-left corner of the page that appears and select Dev Tools.
On the Console tab, run the following command to view the synchronized data:
POST /es_test/_search?pretty { "query": { "match_all": {}} }
NoteYou must replace
es_test
with the index name that you specify when you configure the batch synchronization task.If the data is synchronized, the result shown in the following figure is returned.