Method for synchronizing data to ElasticSearch

By scheduling data synchronization tasks and multi-thread parallel processing, it solves the problems of strong business coupling, hard coding and full data synchronization in data synchronization in the existing technology, and realizes a coupling-free and non-intrusive data synchronization method, supporting single and full data synchronization. , improving synchronization efficiency and flexibility.

CN118535647APending Publication Date: 2024-08-23BEIYIN FINANCIAL TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410747372.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-11
Publication Date
2024-08-23

AI Technical Summary

Technical Problem

When synchronizing data from a relational database to ElasticSearch, the existing technology has problems such as strong business coupling, hard coding problems, only incremental data synchronization, complex binlog system construction, and inability to support multiple table full data synchronization.

Method used

Provide a data synchronization method that schedules data synchronization tasks, uses Linux server timing tasks and Java services, and synchronizes full table data or specified data based on the database table name or primary key name and primary key ID, using multi-threaded parallel processing and paging query. method, synchronize data to ElasticSearch, support the synchronization of single data and full data, and use data coverage to ensure the repeatability of synchronization tasks.

Benefits of technology

It achieves data synchronization without strong business coupling and no hard coding, supports multiple table full data synchronization, improves synchronization efficiency and flexibility, and ensures the repeatability and efficiency of data synchronization tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118535647A
    Figure CN118535647A
Patent Text Reader

Abstract

The invention provides a method and a system for synchronizing data to ElasticSearch. The method for synchronizing the data to ElasticSearch comprises the following steps of: scheduling a data synchronization task; the relational database is used for storing business data; the method comprises the following steps of: synchronizing data in a source database to ElasticSearch; and storing a final result after data writing. Synchronization of single data of a database form and full-amount synchronization of data of the whole table are supported, and a data coverage mode is adopted for both the single data and the full data, so that it is guaranteed that a data synchronization task can be repeatedly executed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data synchronization, and in particular to a method for synchronizing data to ElasticSearch. Background Art

[0002] Elasticsearch is mainly used to implement complex search functions, and can provide full-text retrieval, structured search, data analysis and aggregation functions. In actual project development, relational databases such as Oracle and MySQL are often used as business databases, and Elasticsearch is used as a query database to achieve read-write separation, relieve the query pressure of relational databases, and cope with complex queries on massive data. This involves a key issue: how to synchronize data from relational databases to Elasticsearch.

[0003] Data can be synchronized from a relational database to Elasticsearch through synchronous dual writing, asynchronous dual writing, timestamp-based SQL extraction, and binlog-based real-time synchronization.

[0004] Synchronous dual writing is writing data to Elasticsearch at the same time as writing it to a relational database. This usually requires hard coding at the application layer.

[0005] Asynchronous dual write implements asynchronous multi-source writing through message queues. Data change events are first sent to the message queue, and then consumers consume messages from the message queue and write them to Elasticsearch.

[0006] Timestamp-based SQL extraction is to add a timestamp field to the database table, scan the specified table at a certain period through a timer program, extract the changed data and write it into Elasticsearch.

[0007] Binlog-based real-time synchronization uses the database's binlog for synchronization. By subscribing to binlog logs, the logs are parsed and updated and written to Elasticsearch.

[0008] Currently, data can be synchronized from relational databases to Elasticsearch through synchronous dual writing, asynchronous dual writing, timestamp-based SQL extraction, and binlog-based real-time synchronization. The existing technology has the following problems:

[0009] 1. Synchronous dual writing has strong business coupling, and it is necessary to add Elasticsearch writing code in all places where the database needs to be written;

[0010] 2. Asynchronous dual writing requires the introduction of message middleware and hard coding issues. Connecting to a new data source requires implementing new consumer code.

[0011] 3. SQL extraction based on timestamp can only synchronize incremental data;

[0012] 4. The disadvantage of real-time synchronization based on binlog is that it is complicated to build a binlog system;

[0013] 5. None of the above solutions support multiple table full data synchronization. Summary of the invention

[0014] In view of the above problems, the present invention is proposed to provide a method for synchronizing data to ElasticSearch that overcomes the above problems or at least partially solves the above problems.

[0015] According to one aspect of the present invention, a method for synchronizing data to ElasticSearch is provided, the method comprising:

[0016] Schedule data synchronization tasks;

[0017] A relational database that stores business data;

[0018] Synchronize data from the source database to ElasticSearch;

[0019] Stores the final result after data is written.

[0020] Optionally, synchronizing the data in the source database to ElasticSearch specifically includes:

[0021] Synchronize all table data according to the database table name;

[0022] Synchronize specified data based on database table name, primary key name, and primary key ID.

[0023] Optionally, the synchronization of the entire table data according to the database table name specifically includes:

[0024] Step S1: Linux server scheduled task, call the shell script to execute the curl command and enter the table name;

[0025] Step S2: Call the Java service full table data synchronization method to query the total number of data entries in the table name;

[0026] Step S3: specify the number of threads N;

[0027] Step S4: Calculate the starting number and ending number of each thread according to the total number of threads and the number of threads N;

[0028] Step S5: perform a query every 10,000 records;

[0029] Step S6: Connect to the Oracle database;

[0030] Step S7: Obtain the query result set S;

[0031] Step S8: Determine whether the value of S is less than 10,000. If it is less than 10,000, enter the sub-process:

[0032] Step S9: Determine whether the value of S is less than 10,000. If it is greater than or equal to 10,000, enter the sub-process:

[0033] Return to step S5.

[0034] Optionally, the step S8: determining whether the value of S is less than 10,000, and entering the sub-process when it is less than 10,000 specifically includes:

[0035] Connect to ElasticSearch;

[0036] Write query results to ElasticSearch.

[0037] Optionally, the step S9: determining whether the value of S is less than 10,000, and entering the sub-process when the value of S is greater than or equal to 10,000 specifically includes:

[0038] Connect to ElasticSearch;

[0039] Write query results to ElasticSearch.

[0040] Optionally, the synchronization of designated data according to the database table name, primary key name and primary key ID specifically includes:

[0041] The Linux server scheduled task calls the shell script to execute the curl command and enters the table name, primary key name, and primary key ID;

[0042] Call the Java service single data synchronization method, query a single data, and splice the corresponding database SQL;

[0043] Connect to the Oracle database and query according to SQL;

[0044] Get the query results;

[0045] Connect to ElasticSearch;

[0046] Write query results to ElasticSearch.

[0047] The present invention also provides a data synchronization system for synchronizing to ElasticSearch, wherein the synchronization system applies the above-mentioned data synchronization method for synchronizing to ElasticSearch, and the synchronization system comprises:

[0048] Scheduled task module, used to schedule data synchronization tasks;

[0049] Source database module, a relational database used to store business data;

[0050] Data synchronization module, used to synchronize data from the source database to ElasticSearch;

[0051] ElasticSearch storage module, used to store the final results after data is written.

[0052] The present invention provides a data synchronization method and system for synchronizing to ElasticSearch, the data synchronization method comprising: scheduling data synchronization tasks; storing a relational database of business data; synchronizing data in a source database to ElasticSearch; and storing the final result after the data is written. It supports synchronization of database table data and full synchronization of the entire table data, and whether it is a single row or the full amount, the data overwriting method is adopted to ensure that the data synchronization task can be executed repeatedly.

[0053] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0054] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.

[0055] Figure 1 A flowchart of a method for synchronizing data to ElasticSearch provided by an embodiment of the present invention;

[0056] Figure 2 A block diagram of a data synchronization system for synchronizing to ElasticSearch provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0057] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0058] The terms "comprises" and "having" and any variations thereof in the description embodiments, claims and drawings of the present invention are intended to cover non-exclusive inclusions, for example, including a series of steps or units.

[0059] The technical solution of the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments.

[0060] like Figure 1 As shown, it is a system flow chart of a method for synchronizing data to ElasticSearch provided by an embodiment of the present invention, comprising the following steps:

[0061] When synchronizing specified data, it includes:

[0062] The Linux server scheduled task calls the shell script to execute the curl command and enters the table name, primary key name, and primary key ID;

[0063] Call the Java service single data synchronization method, query a single data, and splice the corresponding database SQL;

[0064] Connect to the Oracle database and query according to SQL;

[0065] Get the query results;

[0066] Connect to ElasticSearch;

[0067] Write query results to ElasticSearch.

[0068] When synchronizing the full amount of data, it specifically includes:

[0069] The Linux server scheduled task calls the shell script to execute the curl command and enter the table name;

[0070] Call the Java service full table data synchronization method to query the total number of data entries in the table name;

[0071] Specify the number of threads N;

[0072] Calculate the starting and ending numbers of each thread based on the total number of lines and the number of threads N;

[0073] Perform one query for every 10,000 records;

[0074] Connect to oracle database;

[0075] Get the query result set S;

[0076] Determine whether the size of S is less than 10,000. If it is less than 10,000, enter the following sub-process:

[0077] Connect to ElasticSearch;

[0078] Write the query results to ElasticSearch;

[0079] Determine whether the size of S is less than 10,000. If it is greater than or equal to 10,000, enter the following sub-process:

[0080] Connect to ElasticSearch;

[0081] Write the query results to ElasticSearch;

[0082] Return to step (5).

[0083] like Figure 2 As shown, a data synchronization system synchronized to ElasticSearch includes: a scheduled task module, a source database module, a data synchronization module and an ElasticSearch module:

[0084] Scheduled task module, used for scheduling data synchronization tasks;

[0085] Source database module, a relational database used to store business data;

[0086] The data synchronization module is used to synchronize the data in the source database to ElasticSearch, synchronize the full data of the table according to the database table name, and synchronize the specified data according to the database table name, primary key name, and primary key ID;

[0087] ElasticSearch module, used to store the final results after data is written.

[0088] Beneficial effects:

[0089] The data synchronization method to ElasticSearch of the present invention is independent of the business, has no strong coupling with the business, does not change the performance of the original program, does not change the original code, is non-invasive, and has no hard coding, that is, it supports the synchronization of database table data and the full synchronization of the entire table data, and whether it is a single item or the full amount, data coverage is used to ensure that the data synchronization task is executed repeatedly.

[0090] Because the amount of data in the database table is sometimes very large, in order to reduce synchronization time and improve synchronization efficiency, multi-threaded parallel processing is used. Even if multi-threading is enabled, the amount of data that each thread needs to synchronize may still be very large, which may cause Java service memory overflow. A paging query is performed every 10,000 records, and then the query results are written to ElasticSearch. In this way, special attention should be paid to the fact that the last query result of less than 10,000 records also needs to be written to ElasticSearch.

[0091] The above specific implementation methods further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for synchronizing data to ElasticSearch, characterized in that: The data synchronization method comprises: Schedule data synchronization tasks; A relational database that stores business data; Synchronize data from the source database to ElasticSearch; Stores the final result after data is written.

2. A method for synchronizing data to ElasticSearch according to claim 1, characterized in that: Synchronizing the data in the source database to ElasticSearch specifically includes: Synchronize all table data according to the database table name; Synchronize specified data based on database table name, primary key name, and primary key ID.

3. A method for synchronizing data to ElasticSearch according to claim 2, characterized in that: The synchronization of the entire table data according to the database table name specifically includes: Step S1: Linux server scheduled task, call the shell script to execute the curl command and enter the table name; Step S2: Call the Java service full table data synchronization method to query the total number of data entries in the table name; Step S3: specify the number of threads N; Step S4: Calculate the starting number and ending number of each thread according to the total number of threads and the number of threads N; Step S5: perform a query every 10,000 records; Step S6: Connect to the Oracle database; Step S7: Obtain the query result set S; Step S8: Determine whether the value of S is less than 10,000. If it is less than 10,000, enter the sub-process: Step S9: Determine whether the value of S is less than 10,000. If it is greater than or equal to 10,000, enter the sub-process: Return to step S5.

4. A method for synchronizing data to ElasticSearch according to claim 3, characterized in that: The step S8: determining whether the value of S is less than 10,000, and if it is less than 10,000, entering the sub-process specifically includes: Connect to ElasticSearch; Write query results to ElasticSearch.

5. A method for synchronizing data to ElasticSearch according to claim 3, characterized in that: The step S9: judging whether the value of S is less than 10,000, and if it is greater than or equal to 10,000, entering the sub-process specifically includes: Connect to ElasticSearch; Write query results to ElasticSearch.

6. A method for synchronizing data to ElasticSearch according to claim 2, characterized in that: The synchronization of designated data according to the database table name, primary key name and primary key ID specifically includes: The Linux server scheduled task calls the shell script to execute the curl command and enters the table name, primary key name, and primary key ID; Call the Java service single data synchronization method, query a single data, and splice the corresponding database SQL; Connect to the Oracle database and query according to SQL; Get the query results; Connect to ElasticSearch; Write query results to ElasticSearch.

7. A data synchronization system synchronized to ElasticSearch, characterized in that: The synchronization system applies a data synchronization method for synchronizing to ElasticSearch according to any one of claims 1 to 6, and the synchronization system includes: Scheduled task module, used to schedule data synchronization tasks; Source database module, a relational database used to store business data; Data synchronization module, used to synchronize data from the source database to ElasticSearch; ElasticSearch storage module, used to store the final results after data is written.