Methods for loading data into relational databases

By adopting the data reading, loading and resuming steps in data replication between heterogeneous databases, the difficulty of data replication between heterogeneous databases is solved, efficient data loading and conversion is achieved, and data quality is improved. and integrity.

CN111414358BActive Publication Date: 2024-09-27杭州美创科技股份有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN201911403002.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-12-30
Publication Date
2024-09-27
Estimated Expiration
2039-12-30

AI Technical Summary

Technical Problem

Existing technology is difficult to achieve efficient data copying and loading between heterogeneous databases, especially when the data format is confusing, the table structure cannot be modified, and filtering and conversion processing cannot be performed during the synchronization process.

Method used

Adopt a method that includes data reading, loading and breakpoint resuming steps, generate SQL statements through data processing, realize batch execution of existing and incremental data, and use configuration to perform DDL conversion to solve data loading conflicts in heterogeneous databases , supports data filtering and transformation, and is suitable for relational databases.

Benefits of technology

It realizes data loading between heterogeneous databases, improves data quality, solves the problem of confusing data format and inability to modify the table structure, and provides a breakpoint resumption mechanism to ensure data integrity and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111414358B
    Figure CN111414358B
Patent Text Reader

Abstract

The present invention discloses a method for loading data in a relational database, comprising the following steps: a data file reading thread reads data files to be loaded into a target database, wherein the data files are divided into stock data files and incremental data files; the stock data loading thread generates SQL statements from the read data files through data processing, executes the statements in batches into the target database, and saves the statements as ckpt data; when the stock loading thread is manually exited or abnormally terminated, after the stock data loading thread is restarted, the data file reading thread continues to read data at the position read last time. The present invention has the characteristics of being able to realize the stock and incremental data loading of a database; and being able to solve the DDL conversion of heterogeneous databases according to the configuration.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of data replication in a database, and in particular to a method for data loading in a relational database, which can realize the loading of existing and incremental data in a database, solve DDL conversion of heterogeneous databases according to configuration, and ensure data loading in heterogeneous databases, and can load data after conversion according to usage scenarios. Background Art

[0002] Traditional data synchronization and replication is implemented by the database's own homogeneous replication technology. When the source table changes, the database engine triggers a synchronization event to synchronize the changed data in the source table to the target database. However, the above replication technology can only be used between homogeneous databases. At present, various database developers have proposed replication solutions for heterogeneous databases based on their own database homogeneous replication. For example, Microsoft SQL Server has proposed a publisher / subscriber synchronization solution, Oracle uses multi-master replication and materialized view solutions to achieve data synchronization, and DB uses CCD tables to achieve replication between heterogeneous databases. Although the auxiliary tools developed by various manufacturers have better improved the function of data synchronization and replication, the replication solution still relies on its own database management system (DBMS) core technology and cannot be guaranteed to be completely independent of DBMS.

[0003] Compared with loading homogeneous databases, loading heterogeneous data has the following disadvantages:

[0004] (1) Databases use various data models to represent and store data, which leads to confusion in data formats and independent access interfaces.

[0005] (2) In some cases, the table structure of the target database cannot be changed. When the table is dynamically generated, the table structure cannot be modified and the database's own replication function cannot be used.

[0006] (3) Data cannot be filtered or converted during the synchronization process. Summary of the invention

[0007] The purpose of the present invention is to overcome the above-mentioned deficiencies in the prior art and to provide a method for data loading in a relational database that can realize the loading of existing and incremental data of a database; can solve the DDL conversion of heterogeneous databases according to the configuration to ensure the loading of data in heterogeneous databases; and can convert and load data according to the usage scenario.

[0008] In order to achieve the above object, the present invention adopts the following technical solutions:

[0009] A method for loading data into a relational database comprises the following steps:

[0010] (1-1) Data reading

[0011] The data file reading thread reads the data files that need to be loaded into the target database. The data files are divided into stock data files and incremental data files.

[0012] (1-2) Data loading

[0013] The stock data loading thread will read the data file, generate SQL statements through data processing, execute them in batches into the target database, and save them as ckpt data;

[0014] (1-3) Resume download

[0015] When the stock loading thread is manually exited or abnormally terminated, after the stock data loading thread is restarted, the data file reading thread continues to read data at the last read position;

[0016] When the incremental loading thread is manually exited or abnormally terminated, after the incremental data loading thread is restarted, the data file reading thread continues to read data at the last read position.

[0017] The present invention can centrally handle conflicts generated during data loading of heterogeneous databases and load the data into the target database. At the same time, the data can be filtered, converted and mapped according to the configuration, thus solving the problem of scattered, messy and non-uniform data in the source data and greatly improving the data quality.

[0018] Preferably, (1-1) comprises the following steps:

[0019] Start the data file reading thread. The data file reading thread obtains the existing data file, reads it in the form of data segments, serializes the read data into the data file in Json format, and obtains the data object through Json deserialization and parsing. The attributes of the data object include the scn value and the source database library type. The source database types include Oracle, MySql and Postgresql.

[0020] Get the source database type according to the data object. If the source database type is an Oracle database and the data currently read is stock data, store the SCN value in the read data into a persistent container and use the SCN value to establish a connection between the stock data and the incremental data.

[0021] If the source database type is an Oracle database and the data currently read is incremental data, first roll back the rows of the Oracle database; then, put the currently read data into the memory, and put the transaction number in the data into the data queue to be loaded.

[0022] Preferably, (1-2) comprises the following steps:

[0023] Each stock loading thread obtains the transaction number in the data queue to be loaded, and obtains all data rows corresponding to the transaction number in the data cache according to the transaction number;

[0024] Each incremental loading thread obtains the transaction number in the data queue to be loaded, obtains the data row corresponding to the transaction number in the data cache according to the transaction number, traverses all data rows, and according to the database type of each data row, if the source database is an Oracle database, hashes the rowld field value to obtain the hash value A11; if the source database is a non-Oracle database, concatenates schemaName, tableName and all primarys values ​​and hashes them to obtain the hash value A12; then uses the current number of loading threads B1, takes A11 or A12 as the dividend and B1 as the divisor to perform a modulus operation to obtain the modulus value C1; and puts each data row into the loading thread corresponding to the modulus value C1 for loading.

[0025] Preferably, the data file reading thread continues to read data at the location of the last read, including a process of resuming the transfer of existing data and a process of resuming the transfer of incremental data;

[0026] The process of resuming existing data includes the following steps:

[0027] Read the data of all existing loading threads in the ckpt table of the target database, obtain all the transaction numbers being loaded and the last submitted transaction numbers, if the transaction number being loaded is the same as the last submitted transaction number, the transaction corresponding to the transaction number does not need to be recovered;

[0028] If the transaction number being loaded is different from the last submitted transaction number, the transaction corresponding to the transaction number is restored;

[0029] By comparing the smallest file offset position and the largest file offset position in the loading thread ckpt, the data file reading thread starts reading the file from the smallest file offset position to determine whether the transaction number in the data row corresponds to the transaction that needs to be recovered;

[0030] If the transaction number in the data row corresponds to the transaction that needs to be recovered, the transaction is placed in the queue of data to be submitted; when the data file reading thread reads to the maximum file offset position, the breakpoint-resume transfer of the existing data is completed.

[0031] Preferably, the incremental data retransmission process includes the following steps:

[0032] When each incremental loading thread is started, the target database is queried for the incremental loading thread last loaded by the current incremental loading thread according to the thread name of the current incremental loading thread, and data is restored at the minimum file offset position and the maximum file offset position;

[0033] The incremental loading thread performs a hash operation on the rowld field value according to the database type of each data row. If the source database is an Oracle database, the hash value A21 is obtained. If the source database is a non-Oracle database, the hash value A22 is obtained by concatenating the schemaName, tableName, and all primarys values. The incremental loading thread then uses the current number of loading threads B2 to perform a modulus operation with A21 or A22 as the dividend and B2 as the divisor to obtain the modulus value C2. Each data row is placed in the loading thread corresponding to the modulus value C2 for loading.

[0034] It is determined whether the data row belongs to the incremental loading thread. If the data row belongs to the incremental loading thread, the data row is put into the data queue. When the data file reading thread reads the maximum file offset position, the incremental data breakpoint resume transmission is completed.

[0035] As a preferred method, using the SCN value to establish the connection between the stock data and the incremental data includes the following steps:

[0036] The data file reading thread saves the existing SCN value of each table A. The incremental loading thread reads the SCN value of table A. If the existing SCN value is ≥ the incremental SCN value, the data row corresponding to the incremental SCN value is discarded. If the existing SCN value is < the incremental SCN value, the data row corresponding to the incremental SCN value is loaded into the target database, and the data row corresponding to the saved SCN value is removed.

[0037] Preferably, (1-2) further includes a DDL loading process:

[0038] According to the database type of the source data and the database type of the standby database, the DDL statement of the standby database is automatically converted through the syntax and lexical analysis of the SQL statement; if the DDL is a statement related to table operations, the data dictionary is reloaded.

[0039] Therefore, the present invention has the following beneficial effects: it can realize the loading of existing and incremental data of the database; it can solve the DDL conversion of heterogeneous databases according to the configuration to ensure the data loading of heterogeneous databases; it can load the data after conversion according to the usage scenario; it can centrally handle the conflicts generated during the data loading process of heterogeneous databases, and load the data into the target database. At the same time, it can filter, convert and map the data according to the configuration, which solves the problem of scattered, messy and non-uniform data in the source data and greatly improves the data quality. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 An overall flow chart of data loading of the present invention;

[0041] Figure 2 A data reading thread flow chart of the present invention;

[0042] Figure 3 The present invention is a data loading thread flow chart. DETAILED DESCRIPTION

[0043] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments.

[0044] like Figure 1 The embodiment shown is a method for loading data into a relational database, comprising the following steps:

[0045] (1-1) Data reading:

[0046] like Figure 2 As shown, the data file reading thread is started, the data file reading thread obtains the stock data file, reads it in the form of data segments, serializes the read data into the data file in Json format, and obtains the data object after Json deserialization and parsing; the attributes of the data object include the scn value and the source database library type; the source database types include Oracle, MySql and Postgresql;

[0047] The properties of the data object are shown in the following table:

[0048]

[0049]

[0050] Get the source database type according to the data object. If the source database type is an Oracle database and the data currently read is stock data, store the SCN value in the read data into a persistent container and use the SCN value to establish a connection between the stock data and the incremental data.

[0051] If the source database type is an Oracle database and the data currently read is incremental data, first roll back the rows of the Oracle database; then, put the currently read data into the memory, and put the transaction number in the data into the data queue to be loaded.

[0052] (1-2) Data loading:

[0053] like Figure 3 As shown, each stock loading thread obtains the transaction number in the data queue to be loaded, and obtains all data rows corresponding to the transaction number in the data cache according to the transaction number;

[0054] Each incremental loading thread obtains the transaction number in the data queue to be loaded, obtains the data row corresponding to the transaction number in the data cache according to the transaction number, traverses all data rows, and according to the database type of each data row, if the source database is an Oracle database, hashes the rowld field value to obtain the hash value A11; if the source database is a non-Oracle database, concatenates schemaName, tableName and all primarys values ​​and hashes them to obtain the hash value A12; then uses the current number of loading threads B1, takes A11 or A12 as the dividend and B1 as the divisor to perform a modulus operation to obtain the modulus value C1; and puts each data row into the loading thread corresponding to the modulus value C1 for loading.

[0055] The steps to establish the connection between stock data and incremental data using SCN values ​​include:

[0056] The data file reading thread saves the existing SCN value of each table A. The incremental loading thread reads the SCN value of table A. If the existing SCN value is ≥ the incremental SCN value, the data row corresponding to the incremental SCN value is discarded. If the existing SCN value is < the incremental SCN value, the data row corresponding to the incremental SCN value is loaded into the target database, and the data row corresponding to the saved SCN value is removed.

[0057] It also includes the DDL loading process:

[0058] According to the database type of the source data and the database type of the standby database, the DDL statement of the standby database is automatically converted through the syntax and lexical analysis of the SQL statement; if the DDL is a statement related to table operations, the data dictionary is reloaded.

[0059] Generate standard executable DML SQL statements according to the type of DML data, load the data dictionary of the target database according to the schema name and table name, convert the data type, and submit the data to the target database after processing. If the data is loaded for stock, and the primary and standby databases are Oracle, the rowid in the source database can be mapped to the rowid of the target database, which can greatly speed up the execution efficiency of update and delete operations. Before loading the stock data, generate the generated insert SQL statement as a statement that can be called back to obtain the Oracle rowid, and persist the rowid in the source database and the rowid obtained by the callback to the storage medium of the specified key-value. When executing incremental update or delete statements, obtain the rowid of the target database in the storage medium through the rowid of the source database, and use the rowid as the only condition of where to execute the SQL statement.

[0060] When the loading thread submits data, it needs to update the ckpt table on the target end at the same time to ensure the integrity of the data. In step 3, data is restored through the ckpt table during breakpoint resumption.

[0061] (1-3) Resume download:

[0062] When the loading process is manually exited or abnormally terminated, after the stock data loading thread is restarted, the data file reading thread continues to read data at the last read position.

[0063] The process of resuming existing data includes the following steps:

[0064] Read the data of all existing loading threads in the ckpt table of the target database, obtain all the transaction numbers being loaded and the last submitted transaction numbers, if the transaction number being loaded is the same as the last submitted transaction number, the transaction corresponding to the transaction number does not need to be recovered;

[0065] If the transaction number being loaded is different from the last submitted transaction number, the transaction corresponding to the transaction number is restored;

[0066] By comparing the smallest file offset position and the largest file offset position in the loading thread ckpt, the data file reading thread starts reading the file from the smallest file offset position to determine whether the transaction number in the data row corresponds to the transaction that needs to be recovered;

[0067] If the transaction number in the data row corresponds to the transaction that needs to be recovered, the transaction is placed in the queue of data to be submitted; when the data file reading thread reads to the maximum file offset position, the breakpoint-resume transfer of the existing data is completed.

[0068] When each incremental loading thread is started, the target database is queried for the incremental loading thread last loaded by the current incremental loading thread according to the thread name of the current incremental loading thread, and data is restored at the minimum file offset position and the maximum file offset position;

[0069] The incremental loading thread performs a hash operation on the rowld field value according to the database type of each data row. If the source database is an Oracle database, the hash value A21 is obtained. If the source database is a non-Oracle database, the hash value A22 is obtained by concatenating the schemaName, tableName, and all primarys values. The incremental loading thread then uses the current number of loading threads B2 to perform a modulus operation with A21 or A22 as the dividend and B2 as the divisor to obtain the modulus value c2. Each data row is placed in the loading thread corresponding to the modulus value c2 for loading.

[0070] It is determined whether the data row belongs to the incremental loading thread. If the data row belongs to the incremental loading thread, the data row is put into the data queue. When the data file reading thread reads the maximum file offset position, the incremental data breakpoint resume transmission is completed.

[0071] It should be understood that the present embodiment is only used to illustrate the present invention and is not used to limit the scope of the present invention. In addition, it should be understood that after reading the content taught by the present invention, those skilled in the art can make various changes or modifications to the present invention, and these equivalent forms also fall within the scope limited by the appended claims of the application.

Claims

1. A method for loading data into a relational database, characterized in that: The steps include: (1-1) Data reading The data file reading thread reads the data files that need to be loaded into the target database. The data files are divided into stock data files and incremental data files; (1-2) Data loading The stock data loading thread reads the data file, generates SQL statements through data processing, executes them in batches into the target database, and saves them as ckpt data; (1-3) Breakpoint Resume When the stock loading thread is manually exited or abnormally terminated, after the stock data loading thread is restarted, the data file reading thread continues to read data at the last read position; when the incremental loading thread is manually exited or abnormally terminated, after the incremental data loading thread is restarted, the data file reading thread continues to read data at the last read position; (1-1) includes the following steps: starting a data file reading thread, the data file reading thread obtains the stock data file, reads it in the form of data segments, serializes the read data into the data file in Json format, and obtains the data object through Json deserialization and parsing; the attributes of the data object include the scn value and the source database library type; the source database types include Oracle, MySql and Postgresql; the source database type is obtained according to the data object, if the source database type is an oracle database, and the data currently read is stock data, then the scn value in the read data is stored in a persistent container, and the scn value is used to establish the connection between the stock data and the incremental data; If the source database type is an Oracle database and the data currently read is incremental data, first roll back the rows of the Oracle database; then put the currently read data into the memory and put the transaction number in the data into the data queue to be loaded; (1-2) includes the following steps: each stock loading thread obtains the transaction number in the data queue to be loaded, and obtains all data rows corresponding to the transaction number in the data cache according to the transaction number; each incremental loading thread obtains the transaction number in the data queue to be loaded, and obtains the data row corresponding to the transaction number in the data cache according to the transaction number, traverses all data rows, and according to the database type of each data row, if the source database is an Oracle database, hashes the rowld field value to obtain a hash value A11; if the source database is a non-Oracle database, concatenates schemaName, tableName and all primary values ​​and hashes them to obtain a hash value A12; then uses the current number of loading threads B1, takes A11 or A12 as the dividend and B1 as the divisor to perform a modulus operation to obtain a modulus value C1; puts each data row into the loading thread corresponding to the modulus value C1 for loading; Using the scn value to establish the connection between the stock data and the incremental data includes the following steps: the stock scn value of each table A is saved in the data file reading thread, and the incremental loading thread reads the scn value of table A. If the stock scn value is greater than or equal to the incremental scn value, the data row corresponding to the incremental scn value is discarded; if the stock scn value is less than the incremental scn value, the data row corresponding to the incremental scn value is loaded into the target database, and the data row corresponding to the saved scn value is removed; 1-2) It also includes the DDL loading process: according to the database type of the source data and the database type of the standby database, the DDL statements of the standby database are automatically converted through the syntax and lexical analysis of the SQL statements; if the DDL is a statement related to table operations, the data dictionary is reloaded; The data file reading thread continues to read data at the location where it was last read, including the existing data retransmission process and the incremental data retransmission process; the existing data retransmission process includes the following steps: reading the data of all existing loading threads in the ckpt table of the target database, obtaining all the transaction numbers being loaded and the last submitted transaction number, if the loading transaction number is the same as the last submitted transaction number, the transaction corresponding to the transaction number does not need to be recovered; If the transaction number being loaded is different from the last submitted transaction number, the transaction corresponding to the transaction number is restored; by comparing the smallest file offset position and the largest file offset position in the loading thread ckpt, the data file reading thread starts reading the file from the smallest file offset position to determine whether the transaction number in the data row corresponds to the transaction that needs to be restored; If the transaction number in the data row corresponds to the transaction that needs to be recovered, the transaction is placed in the queue of data to be submitted; When the data file reading thread reads the maximum file offset position, the existing data breakpoint resume transmission is completed; The incremental data retransmission process includes the following steps: when each incremental loading thread is started, the incremental loading thread last loaded by the current incremental loading thread is queried from the target database according to the thread name of the current incremental loading thread, and data is restored at the minimum file offset position and the maximum file offset position; The incremental loading thread performs a hash operation on the rowld field value according to the database type of each data row. If the source database is an Oracle database, the hash value A21 is obtained. If the source database is a non-Oracle database, the hash value A22 is obtained by concatenating the schemaName, tableName, and all primarys values. The incremental loading thread then performs a modulus operation using the current number of loading threads B2, A21 or A22 as the dividend, and B2 as the divisor to obtain the modulus value C2. Place each data row in the loading thread corresponding to the module value C2 for loading; determine whether the data row belongs to the incremental loading thread. If the data row belongs to the incremental loading thread, place the data row in the data queue. When the data file reading thread reads the maximum file offset position, the incremental data breakpoint resume is completed.