Java(120):使用Java对TiDB数据库多批次批量写入数据

使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值