This topic describes how to use a program to import data into PolarDB-X.
The schema of the example source table is as follows:
CREATE TABLE `sourcedb` (
`id` int(11) NOT NULL,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_1` (`k`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 dbpartition by hash(`id`);Export data from a database
You can import data from various sources, including a database. For example, you can run the mysql -e command to export data from a MySQL database:
mysql -h ip -P port -u usr -pPassword db_name -N -e "SELECT id,k,c,pad FROM sourcedb;" >/home/data_1000w.txt
## The values in the source data file are separated by tab characters: 188092293 27267211 59775766593-64673028018-...-09474402685 01705051424-...-54211554755.
mysql -h ip -P port -u usr -pPassword db_name -N -e "SELECT id,k,c,pad FROM sourcedb;" | sed 's/\t/,/g' >/home/data_1000w.csv
## The values in a CSV file are separated by commas: 188092293, 27267211, 59775766593-64673028018-...-09474402685, 01705051424-...-54211554755We recommend converting your source data file to the CSV format, which can be read by your program more efficiently.
Create a table in PolarDB-X
As the source file does not contain the CREATE TABLE statement, you need to create a table in the destination PolarDB-X database to hold data that is exported from the source file:
CREATE TABLE `targetdb` (
`id` int(11) NOT NULL,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_1` (`k`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 dbpartition by hash(`id`);Import data to PolarDB-X using a program
You can write a program that connects to a PolarDB-X database. This program can read data from the source file and import the data to the PolarDB-X database using batch inserts.
The following is the code of an example Java program:
// mysql-connector-java.jar is required. For more information, visit https://mvnrepository.com/artifact/mysql/mysql-connector-java.
// Download link: https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.20/mysql-connector-java-8.0.20.jar.
// Note: Different versions of mysql-connector-java.jar contain Class.forName("com.mysql.cj.jdbc.Driver") class in different paths.
// Compile javac LoadData.java.
// Run java -cp .:mysql-connector-java-8.0.20.jar LoadData
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class LoadData {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
File dataFile = new File("/home/data_1000w.csv");
String sql = "insert into targetdb(id, k, c, pad) values(?, ?, ?, ?)";
int batchSize = 1000;
try (
Connection connection = getConnection("ip", 3306, "db", "usr", "password");
BufferedReader br = new BufferedReader(new FileReader(dataFile))) {
String line;
PreparedStatement st = connection.prepareStatement(sql);
long startTime = System.currentTimeMillis();
int batchCount = 0;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
st.setInt(1, Integer.valueOf(data[0]));
st.setInt(2, Integer.valueOf(data[1]));
st.setObject(3, data[2]);
st.setObject(4, data[3]);
st.addBatch();
if (++batchCount % batchSize == 0) {
st.executeBatch();
System.out.println(String.format("insert %d records", batchCount));
}
}
if (batchCount % batchSize != 0) {
st.executeBatch();
}
long cost = System.currentTimeMillis() - startTime;
System.out.println(String.format("Take %d second,insert %d records, tps %d", cost/1000, batchCount, batchCount/(cost/1000)));
}
}
/**
* Establish a connection with the destination PolarDB-X database.
*
* @param host The IP address of the destination PolarDB-X database.
* @param The port number of the destination PolarDB-X database.
* @param database The name of the destination PolarDB-X database.
* @param username The username of the account that is used to connect to the destination PolarDB-X database.
* @param password The password of the account that is used to connect to the destination PolarDB-X database.
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
private static Connection getConnection(String host, int port, String database, String username, String password)
throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = String.format(
"jdbc:mysql://%s:%d/%s?autoReconnect=true&socketTimeout=600000&rewriteBatchedStatements=true", host, port,
database);
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
}To efficiently import your data into PolarDB-X, you can customize your program by adjusting the batch size and leveraging multithreading.