我们正在尝试使用 JPA 流从 postgraes sql 流式传输数百万条数据库记录,并下载为 Excel(使用 Apache poi 创建 Excel,其他库也可以)。
类似 csv 的解决方案:https://www.geekyhacker.com/2019/03/26/high-performance-data-fetching-using-spring-data-jpa-stream/
示例片段:
BookRepository 类:
@QueryHints(value = {
@QueryHint(name = HINT_FETCH_SIZE, value = "10000")
})
@Query("select b from Book")
Stream<Book> getAll();
控制器:
@Transactional(readOnly = true)
public void processBooks() {
Stream<Book> bookStream = bookRepository.getAll();
OutputStream outputStream = response.getOutputStream();
bookStream.forEach(book -> {
//Excel file rows and workbook creation and streaming here
workbook.write(outputStream);//getting stream closed error
});
}
建议的参考并未考虑重复打开和关闭 Apache POI 导致的高内存使用率。相反,使用分页获取数据,将其存储在内存中,然后立即将其写入 Apache POI SXSSFWorkbook,如下所示,会消耗更少的内存。