使用Java对TiDB数据库批量写入数据
1、前言:
本次对TiDB数据库测试需要1w条数据,如果MySQL可用存储过程造数,结果发现TiDB用不了。只能想其他办法,一种是Java直接批量插入,一种是Jmeter插入。这里用的Java插入。
如果是MySQL用存储过程,可参考:使用MySQL存储过程给表批量造数据_存储过程造数-CSDN博客
2、批量写入数据
2.1、说明:
1、首先url加上:?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&rewriteBatchedStatements=true&useConfigs=maxPerformance&useServerPrepStmts=true
2、使用批量插入,这里是总共插入10000条,一批次1000,10批次插入。
2.2、代码示例
import java.sql.*;
import java.util.Date;
/**
* Description :
*
* @author : HMF
* Date : Created in 15:00 2024/4/16
* @version :
*/
public class tidbUtil {
public Connection conn;
public tidbUtil() {
//直接过数据库tidb
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://10.1.1.16:4000/demo_hmf?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&rewriteBatchedStatements=true&useConfigs=maxPerformance&useServerPrepStmts=true";
String user = "xxx";
String password = "xxx";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed()) {
System.out.println("数据库驱动:[" + driver + "],url:[" + url + "]");
System.out.println("Open DB Connection success");
}
}catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
tidbUtil tb = new tidbUtil();
try{
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
tb.batchInsert(10000,1000);
tb.DBQuery("select * from USERS where ID=1");
tb.DBQuery("select count(*) from USERS");
System.exit(0);
}
public void batchInsert(int totalSize, int batchSize) {
try {
long start = System.currentTimeMillis();
long time = start;
long end ;
String sql ="INSERT INTO USERS(ID,NAME,EMAIL,AGE,SEX,ID_CARD,MOBILE_PHONE,VISA_CARD,OFFICER_CARD,ADDRESS) values(?,'Penny','Penny@163.com',29,'女','131081197803061637',13663776677,'SYP618183458', '军字第00111209号', '北京市朝阳区立水桥北')";
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 1; i <= totalSize; i++)
{
ps.setObject(1, "" + i);
ps.addBatch();
if (i % batchSize == 0)
{
ps.executeBatch();
ps.clearBatch();
end = System.currentTimeMillis();
System.out.println(new Date()+"本批次insert " + batchSize + "条,耗时" + (System.currentTimeMillis() - time) + "毫秒"+((batchSize*1000)/(end - time))+"条/秒");
time = end;
}
}
end = System.currentTimeMillis();
System.out.println("insert " + totalSize + "条总花费的时间:" + (end - start) + "毫秒 "+((totalSize*1000)/(end - start))+"条/秒");
ps.close();
} catch(Exception e) {
e.printStackTrace();
}
}
void DBQuery(String sqlStr) {
try {
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sqlStr);
int columnCount = rs.getMetaData().getColumnCount();
System.out.println("+++++ query sql: "+sqlStr);
while(rs.next()){
String result = "";
for (int i=0; i<columnCount; i++) {
result += "\t" + rs.getString(i+1);
}
System.out.println(result);
}
rs.close();
statement.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
2.3、执行结果:
参考:https://blog.csdn.net/shy_snow/article/details/125047226