【发布时间】: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