Using Elasticsearch to Accelerate Query and Analysis for Relational Databases
This section explains how to synchronize data from a MySQL database to an Elasticsearch cluster in CSS to enable full-text search, ad hoc queries, and statistical analysis of the database.
Scenario
- E-commerce platform: Quickly search for commodities, provide personalized recommendations, and monitor user behavior and transaction data in real time.
- Content management system: Efficiently retrieve a large number of documents and contents, supporting complex queries and data analysis.
- Financial services: Monitor transaction data in real time for risk analysis and fraud detection.
- Social media analysis: Perform sentiment analysis, trend analysis, and influence evaluation on user-generated content.
- Customer relationship management: Quickly search for customer information, analyze customer behavior, and provide customized services.
- Log and event monitoring: Collect and analyze a large amount of log data to monitor system status and security events in real time.
- Healthcare records: Quickly retrieve and analyze patient records to support clinical decision-making and research.
Overview
- Service data is stored in the MySQL database.
- DRS synchronizes data from MySQL databases to the CSS Elasticsearch cluster in real time.
- Users perform full-text search, Ad Hoc query, and statistics analysis in the Elasticsearch cluster.
Advantages
- Improved full-text search capability: Elasticsearch is a search engine that provides powerful full-text search functions. Relational databases are usually not good at full-text retrieval, but Elasticsearch can effectively solve this problem.
- High-concurrency Ad Hoc query: Elasticsearch is designed to process a large number of concurrent query requests, especially in Ad Hoc query scenarios. It can provide fast response to meet query requirements in high-concurrency environments.
- Real-time data synchronization: Data Replication Service (DRS) synchronizes data from the MySQL database to Elasticsearch in real time, ensuring data consistency and real-time performance.
- Simplified data migration and index creation: In Elasticsearch, you can create indexes corresponding to the MySQL database table structure, simplifying data migration and index management.
- Flexible query language: Elasticsearch provides flexible query languages to support complex query construction, such as range queries, fuzzy queries, and aggregation queries. This may require more complex SQL statements in relational databases.
- Statistical analysis: The aggregation function of Elasticsearch can quickly collect and analyze data, such as age distribution statistics, which may require more computing resources and time in relational databases.
- Security and stability: You can configure Elasticsearch clusters and MySQL databases in security mode and use SSL connections to ensure data transmission security and system stability.
- Easy monitoring and maintenance: Elasticsearch provides various monitoring tools and APIs to facilitate system maintenance and performance monitoring.
- Scalability: The Elasticsearch cluster can be horizontally expanded based on service requirements. More nodes can be added to process larger data volumes and query loads.
These advantages make the Elasticsearch cluster an effective supplement to relational databases in processing full-text retrieval and high-concurrency Ad Hoc queries.
Prerequisites
- A MySQL database and an Elasticsearch cluster in security mode are available, and they are in the same VPC and security group.
- Data to be synchronized exists in the MySQL database.
This document uses the following table structure and initial data as an example.
- Create a student information table in MySQL.
CREATE TABLE `student` ( `dsc` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL, `age` smallint unsigned DEFAULT NULL, `name` varchar(32) COLLATE utf8mb4_general_ci NOT NULL, `id` int unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
- Insert the initial data of three students into the MySQL database.
INSERT INTO student (id,name,age,dsc) VALUES ('1','Jack Ma Yun','50','Jack Ma Yun is a business magnate, investor and philanthropist.'), ('2','will smith','22','also known by his stage name the Fresh Prince, is an actor, rapper, and producer.'), ('3','James Francis Cameron','68','the director of avatar');
- Create a student information table in MySQL.
- An index has been created in the Elasticsearch cluster and match the table indexes in the MySQL database.
Run the following command to create an index for the Elasticsearch cluster:
PUT student { "settings": { "number_of_replicas": 0, "number_of_shards": 3 }, "mappings": { "properties": { "id": { "type": "keyword" }, "name": { "type": "short" }, "age": { "type": "short" }, "desc": { "type": "text" } } } }
Configure number_of_shards and number_of_replicas as needed.
Procedure
- Use DRS to synchronize MySQL data to CSS in real time. For details, see From MySQL to CSS/ES.
In this example, configure the parameters by following the suggestions in Table 1.
Table 1 Synchronization parameters Module
Parameter
Suggestion
Create Synchronization Instance > Synchronize Instance Details
Network Type
Select VPC.
Source DB Instance
Select the RDS for MySQL instance to be synchronized, that is, the MySQL database that stores service data.
Synchronization Instance Subnet
Select the subnet where the synchronization instance is located. You are advised to select the subnet where the database instance and the Elasticsearch cluster are located.
Configure Source and Destination Databases > Destination Database
VPC
Select the same VPC as the Elasticsearch cluster.
Subnet
Select the same subnet as the Elasticsearch cluster.
IP Address or Domain Name
Enter the IP address of the Elasticsearch cluster. For details, see Obtaining the IP address of a CSS cluster.
Database Username
Enter the administrator username (admin) and password of the Elasticsearch cluster.
Database Password
Enter the administrator password of the Elasticsearch cluster.
Encryption Certificate
Select the security certificate of the Elasticsearch cluster. If SSL Connection is not enabled, you do not need to select any certificate. For details, see Obtaining the security certificate of a CSS cluster.
Set Synchronization Task
Flow Control
Select No.
Synchronization Object Type
Deselect Table structure, because the indexes matching MySQL tables have been created in the Elasticsearch cluster.
Synchronization Object
Select Tables. Select the database and table name corresponding to the Elasticsearch cluster.
NOTE:Ensure the type name in the configuration item is the same as the index name, that is, _doc.
Process Data
-
Click Next.
After the synchronization task is started, wait until the Status of the task changes from Full synchronization to Incremental, indicating real-time synchronization has started.
- Check the synchronization status of the database.
- Verify full data synchronization.
Run the following command in Kibana of the Elasticsearch cluster to check whether full data has been synchronized to CSS:
GET student/_search
- Insert new data in the source cluster and check whether the data is synchronized to Elasticsearch.
For example, insert a record whose id is 4 in the source cluster.
INSERT INTO student (id,name,age,dsc) VALUES ('4','Bill Gates','50','Gates III is a business magnate, software developer, investor, author, and philanthropist.')
Run the following command in Kibana of the Elasticsearch cluster to check whether new data has been synchronized to CSS:
GET student/_search
- Update data in the source cluster and check whether the data is synchronized to Elasticsearch.
For example, in the record whose id is 4, change the value of age from 50 to 55.
UPDATE student set age='55' WHERE id=4;
Run the following command in Kibana of the Elasticsearch cluster to check whether the data is updated in CSS:
GET student/_search
- Delete data from the source cluster and check whether the data is deleted synchronously from Elasticsearch.
Run the following command in Kibana of the Elasticsearch cluster to check whether the data is deleted synchronously from CSS:
GET student/_search
- Verify full data synchronization.
- Verify the full-text search capability of the database.
For example, run the following command to query the data that contains avatar in dsc in the Elasticsearch cluster:
GET student/_search { "query": { "match": { "dsc": "avatar" } } }
- Verify the ad hoc query capability of the database.
For example, query philanthropist whose age is greater than 40 in the Elasticsearch cluster.
GET student/_search { "query": { "bool": { "must": [ { "match": { "dsc": "philanthropist" } }, { "range": { "age": { "gte": 40 } } } ] } } }
- Verify the statistical analysis capability of the database.
For example, collect statistics on the age distributions of all users in the Elasticsearch cluster.
GET student/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "age_count": { "terms": { "field": "age", "size": 10 } } } }
Other Operations
- Obtaining the IP address of a CSS cluster
- In the navigation pane on the left, choose Clusters.
- In the cluster list, obtain the IP address of a CSS cluster from the Private Network Address column. Generally, the IP address format is <host>:<port> or <host>:<port>,<host>:<port>.
If the cluster has only one node, the IP address and port number of only one node are displayed, for example, 10.62.179.32:9200. If the cluster has multiple nodes, the IP addresses and port numbers of all nodes are displayed, for example, 10.62.179.32:9200,10.62.179.33:9200.
- Obtaining the security certificate of a CSS cluster
- Log in to the CSS management console.
- In the navigation pane, choose Clusters. The cluster list is displayed.
- Click a cluster name to go to the cluster details page.
- In the Configuration area, click Download Certificate next to HTTPS Access.
Figure 2 Download Certificate
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot