【问题标题】:What is the best way to fetch millions of rows at a time in spring boot?在 Spring Boot 中一次获取数百万行的最佳方法是什么?
【发布时间】:2021-04-12 17:07:18
【问题描述】:

我有一个 Spring Boot 应用程序,对于某个特定功能,我必须每天准备一个 CSV 以供其他服务使用。该作业每天早上 6 点运行。并将 csv 转储到服务器上。问题是数据列表很大。它大约有 780 万行。我正在使用 spring JPA 来获取所有记录。他们有更好的方法来提高效率吗?这是我的代码....

@Scheduled(cron = "0 1 6 * * ?")
public void saveMasterChildList() {

    log.debug("running write job");
    DateFormat dateFormatter = new SimpleDateFormat("dd_MM_yy");
    String currentDateTime = dateFormatter.format(new Date());

    String fileName = currentDateTime + "_Master_Child.csv";
    ICsvBeanWriter beanWriter = null;
    List<MasterChild> masterChildren = masterChildRepository.findByMsisdnIsNotNull();
    try {
        beanWriter = new CsvBeanWriter(new FileWriter(new File("/u01/edw_bill/", fileName)),
            CsvPreference.STANDARD_PREFERENCE);
        String[] header = {"msisdn"};
        String[] nameMapping = {"msisdn"};
        beanWriter.writeHeader(header);
        for (MasterChild masterChild : masterChildren) {
            beanWriter.write(masterChild, nameMapping);
        }
    } catch ( IOException e) {
        log.debug("Error writing the CSV file {}", e.toString());
    } finally {
        if (beanWriter != null) {
            try {
                beanWriter.close();
            } catch (IOException e) {
                log.debug("Error closing the writer {}", e.toString());
            }
        }
    }

} here

【问题讨论】:

    标签: spring spring-boot csv jpa spring-data-jpa


    【解决方案1】:

    您可以使用分页来分隔数据并逐块加载。见this

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 1970-01-01
      • 2013-11-11
      • 2015-12-31
      • 1970-01-01
      • 2019-02-15
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多