java实现大文件导出的实现与优化

 更新时间:2023年11月23日 10:08:24   作者:淡抹心痕  
这篇文章主要为大家详细介绍了java实现大文件导出的实现与优化的相关资料,文中的示例代码讲解详细,对我们深入了解java有一定的帮助,感兴趣的小伙伴可以了解下

关于大文件导出的优化迭代情况如下:

计算机配置:四核16G内存

初始版本为单线程单文件导出文件,mybatis读 opencsv写,耗时将近三小时;

第一轮优化改为多线程单文件,提高读数据效率,时间仅缩减十分钟;

第二轮改为多线程多文件,提高写文件效率,时间缩减一个半小时;

第三轮使用 Mybatis 流式查询,并改用 Map 封装数据,提高内存利用率,时间缩减十分钟;

第四轮弃用 Mybatis ,改用原生 JDBC 获取数据并直接拼接,时间缩减十分钟;

第五轮弃用 opencsv ,改用 BufferWriter 直接写数据,时间缩减十分钟;

输出:

2023-04-23 22:01:30 [main] INFO  WriteData - 单线程单文件 total time in 258s
2023-04-23 22:02:44 [main] INFO  WriteData - 固定线程单文件 total time in 74s
2023-04-23 22:03:40 [main] INFO  WriteData - 固定线程多文件 total time in 55s
2023-04-23 22:04:18 [main] INFO  WriteData - concurrentWrite total time in 37s

2023-04-23 22:26:28 [Thread-1] INFO  WriteData - query in 42s
2023-04-23 22:26:28 [Thread-3] INFO  WriteData - query in 42s
2023-04-23 22:26:28 [Thread-4] INFO  WriteData - query in 42s
2023-04-23 22:26:28 [Thread-6] INFO  WriteData - query in 42s
2023-04-23 22:26:28 [Thread-7] INFO  WriteData - query in 42s
2023-04-23 22:26:28 [Thread-2] INFO  WriteData - query in 42s
2023-04-23 22:26:28 [Thread-5] INFO  WriteData - query in 42s
2023-04-23 22:26:30 [Thread-0] INFO  WriteData - query in 44s

2023-04-23 22:27:00 [Thread-5] INFO  WriteData - write in 31s
2023-04-23 22:27:00 [Thread-1] INFO  WriteData - write in 31s
2023-04-23 22:27:00 [Thread-7] INFO  WriteData - write in 31s
2023-04-23 22:27:00 [Thread-2] INFO  WriteData - write in 31s
2023-04-23 22:27:00 [Thread-3] INFO  WriteData - write in 32s
2023-04-23 22:27:00 [Thread-6] INFO  WriteData - write in 32s
2023-04-23 22:27:00 [Thread-4] INFO  WriteData - write in 32s
2023-04-23 22:27:01 [Thread-0] INFO  WriteData - write in 31s

2023-04-23 22:27:01 [main] INFO  WriteData - 固定线程单文件 total time in 75s

2023-04-23 22:27:24 [Thread-14] INFO  WriteData - query in 22s
2023-04-23 22:27:24 [Thread-13] INFO  WriteData - query in 22s
2023-04-23 22:27:24 [Thread-12] INFO  WriteData - query in 22s
2023-04-23 22:27:24 [Thread-9] INFO  WriteData - query in 22s
2023-04-23 22:27:24 [Thread-11] INFO  WriteData - query in 22s
2023-04-23 22:27:24 [Thread-10] INFO  WriteData - query in 22s
2023-04-23 22:27:24 [Thread-15] INFO  WriteData - query in 22s
2023-04-23 22:27:25 [Thread-8] INFO  WriteData - query in 23s

2023-04-23 22:27:55 [Thread-12] INFO  WriteData - write in 31s
2023-04-23 22:27:55 [Thread-14] INFO  WriteData - write in 31s
2023-04-23 22:27:55 [Thread-9] INFO  WriteData - write in 31s
2023-04-23 22:27:55 [Thread-11] INFO  WriteData - write in 31s
2023-04-23 22:27:55 [Thread-13] INFO  WriteData - write in 31s
2023-04-23 22:27:56 [Thread-15] INFO  WriteData - write in 31s
2023-04-23 22:27:56 [Thread-10] INFO  WriteData - write in 31s
2023-04-23 22:27:56 [Thread-8] INFO  WriteData - write in 31s

2023-04-23 22:27:56 [main] INFO  WriteData - 固定线程多文件 total time in 54s

示例代码

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
import java.time.Duration;
import java.time.LocalDate;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WriteData {

    static final Logger log = LoggerFactory.getLogger(WriteData.class);
    public static final String PARENT_PATH = "C:\\Users\\qiu01\\Desktop\\server\\docker\\mysql\\master\\data\\stu_data\\";
    public static final String URL = "jdbc:mysql://localhost:3307/stu?allowPublicKeyRetrieval=TRUE&useCursorFetch=true";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "123456";
    public static final String SQL = "SELECT * FROM student WHERE id > ? AND id <= ?";
    public static final int TOTAL = 10000000;

    public static final ThreadPoolExecutor POOL = new ThreadPoolExecutor(8, 9, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
    public static final HikariDataSource DS;

    static {
        HikariConfig config = new HikariConfig();

        config.setJdbcUrl(URL);
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);

        DS = new HikariDataSource(config);
    }


    public static void main(String[] args) {
    	// 单线程写文件
        singleThreadWrite();
        // 固定线程写同
        concurrentWriteWithFixedThread(true);
        concurrentWriteWithFixedThread(false);
        concurrentWrite();
    }

    public static void singleThreadWrite() {
        String file = PARENT_PATH + "file.csv";
        long start = System.currentTimeMillis();
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(file))));
             Connection connection = DS.getConnection();
             PreparedStatement stmt = connection.prepareStatement(SQL,ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);) {

            stmt.setFetchSize(10000);
            stmt.setFetchDirection(ResultSet.FETCH_REVERSE);
            stmt.setInt(1, 0);
            stmt.setInt(2, 10000000);

            ResultSet rs = stmt.executeQuery();
            writeToFile(writer, rs);
        } catch (SQLException | IOException e) {
            throw new RuntimeException(e);
        }
        log.info("单线程单文件 total time in {}s", getSeconds(start));
        emptyFolder();
    }


    private static void concurrentWriteWithFixedThread(boolean writeInOneFile) {
        int batch_size = 1250000;
        Thread[] threads = new Thread[TOTAL/batch_size];
        long start = System.currentTimeMillis();
        for (int i = 0; i < TOTAL; i = i + batch_size) {
            final int j = i;
            int no = i / batch_size;

            Thread t = new Thread(() -> {
                String file;
                if (writeInOneFile) {
                    file = PARENT_PATH + "file.csv";
                } else {
                    file = PARENT_PATH + "file_" + no + ".csv";
                }

                try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
                     Connection connection = DS.getConnection();
                     PreparedStatement stmt = connection.prepareStatement(SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
                ) {
                    stmt.setFetchSize(10000);
                    stmt.setFetchDirection(ResultSet.FETCH_REVERSE);
                    stmt.setInt(1, j);
                    stmt.setInt(2, j + batch_size);
                    long queryStart = System.currentTimeMillis();
                    try (ResultSet rs = stmt.executeQuery()) {
                        log.info("query in {}s", getSeconds(queryStart));
                        long writeStart = System.currentTimeMillis();
                        writeToFile(writer, rs);
                        log.info("write in {}s", getSeconds(writeStart));
                    }
                } catch (SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
            threads[no] = t;
        }
        for (Thread t : threads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        if (writeInOneFile) {
            log.info("固定线程单文件 total time in {}s", getSeconds(start));
        } else {
            log.info("固定线程多文件 total time in {}s", getSeconds(start));
        }
//        emptyFolder();
    }

    private static void concurrentWrite() {
        int batch_size = 10000;
        CompletableFuture<Void>[] futures = new CompletableFuture[TOTAL/batch_size];
        long start = System.currentTimeMillis();
        for (int i = 0; i < TOTAL; i = i + batch_size) {
            final int j = i;
            int no = i / batch_size;

            CompletableFuture<Void> t = CompletableFuture.runAsync(() -> {
                String file = PARENT_PATH + "file_" + no + ".csv";

                try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(file))));
                     Connection connection = DS.getConnection();
                     PreparedStatement stmt = connection.prepareStatement(SQL,ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
                     ) {
                    stmt.setInt(1, j);
                    stmt.setInt(2, j + batch_size);
                    try (ResultSet rs = stmt.executeQuery()){
                        writeToFile(writer, rs);
                    }
                } catch (SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            },POOL);
            futures[no] = t;
        }
        CompletableFuture.allOf(futures).join();
        log.info("多线程多文件 total time in {}s", getSeconds(start));
        POOL.shutdown();
        emptyFolder();
    }

    private static void emptyFolder() {
        File file = new File(PARENT_PATH);
        File[] files = file.listFiles();
        for (File f : files) {
            f.delete();
        }
    }

    private static void writeToFile(BufferedWriter writer, ResultSet rs) throws SQLException, IOException {
        StringBuilder builder = new StringBuilder();
        while (rs.next()) {
            String firstName = rs.getString("first_name");
            String lastName = rs.getString("last_name");
            LocalDate dob = rs.getDate("date_of_birth").toLocalDate();
            String gender = rs.getString("gender");
            String email = rs.getString("email");
            String phone = rs.getString("phone_number");
            String address = rs.getString("address");
            String city = rs.getString("city");
            String state = rs.getString("state");
            String zip = rs.getString("zip_code");
            String country = rs.getString("country");
            String nationality = rs.getString("nationality");
            String religion = rs.getString("religion");
            String emergencyContactName = rs.getString("emergency_contact_name");
            String emergencyContactPhone = rs.getString("emergency_contact_phone_number");
            String guardianName = rs.getString("guardian_name");
            String guardianPhone = rs.getString("guardian_phone_number");
            String highSchoolName = rs.getString("high_school_name");
            double highSchoolGpa = rs.getDouble("high_school_gpa");
            int highSchoolGradYear = rs.getInt("high_school_graduation_year");
            String major = rs.getString("major");
            String degreeLevel = rs.getString("degree_level");
            String enrollmentStatus = rs.getString("enrollment_status");

            builder.append(firstName).append("|");
            builder.append(lastName).append("|");
            builder.append(dob).append("|");
            builder.append(gender).append("|");
            builder.append(email).append("|");
            builder.append(phone).append("|");
            builder.append(address).append("|");
            builder.append(city).append("|");
            builder.append(state).append("|");
            builder.append(zip).append("|");
            builder.append(country).append("|");
            builder.append(nationality).append("|");
            builder.append(religion).append("|");
            builder.append(emergencyContactName).append("|");
            builder.append(emergencyContactPhone).append("|");
            builder.append(guardianName).append("|");
            builder.append(guardianPhone).append("|");
            builder.append(highSchoolName).append("|");
            builder.append(highSchoolGpa).append("|");
            builder.append(highSchoolGradYear).append("|");
            builder.append(major).append("|");
            builder.append(degreeLevel).append("|");
            builder.append(enrollmentStatus).append("\n");

            writer.write(builder.toString());
            builder.delete(0, builder.length());
        }
    }

    private static long getSeconds(long start) {
        return Duration.ofMillis(System.currentTimeMillis() - start).getSeconds();
    }
}

以上就是java实现大文件导出的实现与优化的详细内容,更多关于java文件导出的资料请关注脚本之家其它相关文章!

相关文章

  • JDK14之jpackage打包命令的使用

    JDK14之jpackage打包命令的使用

    这篇文章主要介绍了JDK14之jpackage打包命令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Java Ribbon与openfeign区别和用法讲解

    Java Ribbon与openfeign区别和用法讲解

    Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具,主要功能是提供客户端的软件负载均衡算法和服务调用。openfeign对Feign进行了增强,使其支持Spring MVC注解,另外还整合了Ribbon和Nacos,从而使得Feign的使用更加方便
    2022-08-08
  • Java并发编程之Semaphore详解

    Java并发编程之Semaphore详解

    这篇文章主要介绍了Java并发编程之Semaphore详解,Semaphore信号量可以用来控制同时访问特定资源的线程数量,常用于限流场景,Semaphore接收一个int整型值,表示 许可证数量,需要的朋友可以参考下
    2023-11-11
  • idea gradle项目复制依赖小技巧(推荐)

    idea gradle项目复制依赖小技巧(推荐)

    这篇文章主要介绍了idea gradle项目复制依赖小技巧,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • java实现简单验证码生成

    java实现简单验证码生成

    这篇文章主要介绍了java实现简单验证码生成,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • 如何解决Maven下载的依赖版本和引入的依赖版本不一致问题

    如何解决Maven下载的依赖版本和引入的依赖版本不一致问题

    这篇文章主要介绍了如何解决Maven下载的依赖版本和引入的依赖版本不一致问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • IDEA使用JDBC安装配置jar包连接MySQL数据库

    IDEA使用JDBC安装配置jar包连接MySQL数据库

    这篇文章介绍了IDEA使用JDBC安装配置jar包连接MySQL数据库的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • Java的方法和this关键字如何理解与应用

    Java的方法和this关键字如何理解与应用

    Java语言中的“方法”(Method)在其他语言当中也可能被称为“函数”(Function)。对于一些复杂的代码逻辑,如果希望重复使用这些代码,并且做到“随时任意使用”,那么就可以将这些代码放在一个大括号{}当中,并且起一个名字。使用代码的时候,直接找到名字调用即可
    2021-10-10
  • spring cloud alibaba Nacos 注册中心搭建过程详解

    spring cloud alibaba Nacos 注册中心搭建过程详解

    这篇文章主要介绍了spring cloud alibaba Nacos 注册中心搭建过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java 8 引入lambda表达式的原因解析

    Java 8 引入lambda表达式的原因解析

    这篇文章主要介绍了Java 8 引入lambda表达式的原因解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08

最新评论