Overview
Concepts
Cluster Architecture
Strengths
Scenarios
* INSERT INTO table SELECT ...* INSERT INTO table VALUES(...)
INSERT INTO example_tbl (col1, col2, col3) VALUES (1000, "test", 3.25);
Frequent INSERT operations will generate a large number of small files in the storage layer, severely affecting system performance.INSERT INTO example_tbl VALUES(1000, "baidu1", 3.25)(2000, "baidu2", 4.25)(3000, "baidu3", 5.25);
package demo.doris;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class DorisJDBCDemo {private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";private static final String DB_URL_PATTERN = "jdbc:mysql://%s:%d/%s?rewriteBatchedStatements=true";private static final String HOST = "127.0.0.1"; // Leader Node hostprivate static final int PORT = 9030; // query_port of Leader Nodeprivate static final String DB = "demo";private static final String TBL = "test_1";private static final String USER = "admin";private static final String PASSWD = "my_pass";private static final int INSERT_BATCH_SIZE = 10000;public static void main(String[] args) {insert();}private static void insert() {// make sure there are no semicolons (";") at the endString query = "insert into " + TBL + " values(?, ?)";// Setting a Label to achieve idempotence.// String query = "insert into " + TBL + " WITH LABEL my_label values(?, ?)";Connection conn = null;PreparedStatement stmt = null;String dbUrl = String.format(DB_URL_PATTERN, HOST, PORT, DB);try {Class.forName(JDBC_DRIVER);conn = DriverManager.getConnection(dbUrl, USER, PASSWD);stmt = conn.prepareStatement(query);for (int i =0; i < INSERT_BATCH_SIZE; i++) {stmt.setInt(1, i);stmt.setInt(2, i * 100);stmt.addBatch();}int[] res = stmt.executeBatch();System.out.println(res);} catch (Exception e) {e.printStackTrace();} finally {try {if (stmt != null) {stmt.close();}} catch (SQLException se2) {se2.printStackTrace();}try {if (conn != null) conn.close();} catch (SQLException se) {se.printStackTrace();}}}}
rewriteBatchedStatements=true parameter to the JDBC connection string and use the PreparedStatement method.
Currently, Doris does not support server-side PrepareStatemnt, so the JDBC Driver will batch prepare on the client side.
rewriteBatchedStatements=true ensures that the Driver performs batch processing and eventually sends the following INSERT statement to Doris:INSERT INTO example_tbl VALUES(1000, "tencent1", 3.25)(2000, "tencent2", 4.25)(3000, "tencent3", 5.25);
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan