简体   繁体   English

将大数据从DB导出到CSV

[英]Export large data from DB to CSV

I need to export a Table into a CSV File and serve it to download via my JSF/Icefaces Webapplication. 我需要将表导出到CSV文件中并通过我的JSF / Icefaces Web应用程序进行下载。

How can I do that? 我怎样才能做到这一点? I have a Table with 20+ Columns and over 10 MIO rows. 我有一个包含20多列和超过10个MIO行的表。

At the moment, I use a Java Thread, loading all data into ram. 目前,我使用Java Thread,将所有数据加载到ram中。 Then I create a new File and iterate the Collection writing row for row into the file. 然后我创建一个新文件并将Collection的行写入行迭代到文件中。 If the Thread is done, the user can download the large file via Servlet. 如果线程完成,用户可以通过Servlet下载大文件。

But I dont want to write so many GB into ram. 但是我不想把这么多GB写入ram。 I cant secure, not to get a memory problem.. 我无法保证安全,不会出现内存问题。

Is it possible that hibernate does it for me? 是否有可能hibernate为我做这件事? Or does somebody has an other idea? 或者有人有其他想法吗?

Im connected to a DB2 Datebase. 我连接到DB2 Datebase。 The table I want to export is connected to a hibernate bean but it is also possible to write native sql. 我想导出的表连接到一个hibernate bean,但也可以编写本机sql。

Thank you for response! 谢谢你的回复!

Do you need the intermediate stage of a file ? 你需要文件的中间阶段吗? Have you tried loading from the database and writing to your servlet output stream for each row ? 您是否尝试从数据库加载并写入每行的 servlet输出流? That way you're acting simply as a pipe between the client and the db. 这样你只是作为客户端和数据库之间的管道。

Simply set your content-disposition header appropriately and that will signal the client's browser to treat the incoming data as a CSV file itself. 只需适当地设置content-disposition标题,这将指示客户端的浏览器将传入的数据视为CSV文件本身。

I have also gone through with similar kind of problem , the way I solve the problem is that I initially write a CSV file on disc and fetch 25K batch records from DB ans save to the file, and iteratively repeat the process until all the data required by the report is not written on the file. 我也经历过类似的问题,解决问题的方法是我最初在光盘上写一个CSV文件并从DB中获取25K批量记录并保存到文件中,并迭代重复该过程直到所有数据都需要由报告写的不是文件。 And then send the file URL to the client to download the file. 然后将文件URL发送到客户端以下载文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM