使用流读取数据量大的文件并存到mysql数据库中

本文介绍了一种使用Java程序批量将CSV文件中的数据导入到MySQL数据库的方法。通过调整批次大小和数据库连接设置来提高导入效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicLong;

public class MozillaCellImporter {
    protected File file;
    protected String sqlTemplate = "INSERT user (name, age) VALUES (?, ?)";
    protected PreparedStatement statement;

    protected int batchSize = 100000;//默认一次存入数据库数量这里是10W条
    protected String url = "jdbc:mysql://localhost:3306/DBName?serverTimezone=UTC&useSSL=true&rewriteBatchedStatements=true";
    protected String username = "root";
    protected String password = "123456";

    protected Logger logger = LoggerFactory.getLogger(getClass());

    public MozillaCellImporter(String filepath, int batchSize){
        this.file = new File(filepath);
        this.batchSize = batchSize;
        if(!this.file.exists())
            throw new RuntimeException("file not exists: "+filepath);
    }

    public MozillaCellImporter dbSetting(String url, String username, String password){
        this.url = url;
        this.username = username;
        this.password = password;

        return this;
    }

    public String run() throws Exception {
        Connection connection = connection();
        statement = connection.prepareStatement(sqlTemplate);

        AtomicLong lineCount = new AtomicLong(0);
        AtomicLong count = new AtomicLong(0);

        try(BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)){
            reader.lines().forEach(line->{
                lineCount.addAndGet(1);
                if(lineCount.longValue() > 1){去掉第一条数据,根据自己的情况而定
                    try {
                        String temp[] = line.split(",");
                        statement.setInt(1, Integer.valueOf(temp[0]));
                        statement.setInt(2, Integer.valueOf(temp[1]));
                        statement.addBatch();
                        if(lineCount.longValue() % batchSize == 0){
                            count.addAndGet(statement.executeBatch().length);
                            logger.info(String.format("[%15d] insert done, batch size=%d....", lineCount.longValue(), batchSize));
                            statement.clearBatch();
                        }
                    } catch (SQLException e) {
                        logger.error(String.format("error on parse line : %s : %s", line, e.getMessage()));
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        count.addAndGet(statement.executeBatch().length);
        statement.close();
        connection.close();

        return String.format("%d,%d", lineCount.longValue(), count.longValue());
    }

    private Connection connection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection(url, username, password);
    }
}

s是文件的路径

100000是一次存入数据库的数量

返回result格式是  文件数据量,成功存入数据库的数量

String result = null;
try {
     result = new MozillaCellImporter(s, 100000).dbSetting(jdbcUrl, jdbcUsername, jdbcPassword).run();
} catch (Exception e) {
     e.printStackTrace();
}
String[] strings = result.split(",");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值