简体   繁体   English

如何在Java中获取170万条记录?

[英]How to Fetch 1.7 Million records in Java?

I am using MySQL database in which a table has 1.7 million records. 我正在使用MySQL数据库,其中一个表有170万条记录。 Through Restlet framework in Java I want to fetch these records and return it to the client. 通过Java中的Restlet框架,我想获取这些记录并将其返回给客户端。 I am using Linux Centos which is remote server. 我使用的是Linux Centos,它是远程服务器。 I have created WAR file and uploaded on the server. 我创建了WAR文件并上传到服务器上。 When I run the service it is taking lot of time to respond. 当我运行服务时,需要花费大量时间来回应。 I waited for 40 mins but not getting any output. 我等了40分钟但没有得到任何输出。 So Can anybody please help me to resolve this problem? 那么有人可以帮我解决这个问题吗?

That's probably not going to work: holding that many rows of data in memory will probably cause an out of memory exception (can you look at the logs on the server and see what exactly is happening?). 这可能不会起作用:在内存中保留许多行数据可能会导致内存不足异常(你能看一下服务器上的日志,看看到底发生了什么?)。

To do something like this you'll either need to abandon that plan and do pagination of some sort, or you'll need a solution that lets you stream the records to the client without holding them in memory. 要做这样的事情你要么放弃那个计划并做某种分页,要么你需要一个解决方案,让你将记录流式传输到客户端而不将它们保存在内存中。 I'm unsure that the Restlet framework lets you do that: you'll probably need to implement that using servlets yourself. 我不确定Restlet框架是否允许您这样做:您可能需要自己使用servlet来实现它。

When I have a very large number of rows I have used memory mapped files. 当我有非常多的行时,我使用了内存映射文件。 eg I have one database where I have to retrieve and process 1.1 billion rows in around a minute. 例如,我有一个数据库,我必须在大约一分钟内检索和处理11亿行。 (Its over 200 GB) (超过200 GB)

This is a very specialist approach, and I suspect there is a way to tune your SQL database or use a NoSQL database to do what you want. 这是一种非常专业的方法,我怀疑有一种方法可以调整SQL数据库或使用NoSQL数据库来执行您想要的操作。 I would have though you can retrieve 1.7 million in under a minute depending on what you are doing (eg if you are selecting this many amoungst a few TB its going to take a while) 虽然你可以在一分钟内检索170万,具体取决于你正在做什么(例如,如果你选择这个很多的几个TB,它需要一段时间)

But, if there really is no other option, you could write a custom data store. 但是,如果确实没有其他选项,您可以编写自定义数据存储。

BTW: Only a summary is produced. 顺便说一句:只生成摘要。 No one should be expected to read that many rows, certainly not display them in a browser. 不应该期望任何人阅读那么多行,当然不会在浏览器中显示它们。 Perhaps there is something you can do to produce a report or summary so there is less to send the client. 也许您可以做一些事情来生成报告或摘要,因此发送客户端的次数较少。

I have successfully done just this kind of work in my apps. 我在我的应用程序中成功完成了这种工作。 If your client is ready to accept a big response, there is nothing wrong with the approach. 如果您的客户已准备好接受大响应,那么该方法没有任何问题。 The main point is that you'll need to stream the response, which means you can't build te entire response as a string. 重点是您需要流式传输响应,这意味着您无法将整个响应构建为字符串。 Get the outputstream of the HTTP response and write records into it one by one. 获取HTTP响应的输出流并逐个写入记录。 On the db-end you need to set up a scrollable resultset (easy to do at the JDBC level, as well as at the Hibernate level). 在db-end上,您需要设置一个可滚动的结果集(在JDBC级别以及Hibernate级别都很容易)。

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

相关问题 大约有100万条记录时如何在Java中比较Hive和Cassandra数据 - How to compare Hive and Cassandra data in Java when there are around 1 million records 如何通过Java程序更快地选择和插入百万条记录 - How to do select and insert million records through java program faster 如何使用Java在Excel中读取100万行记录 - How to read 1 million records of rows in excel using java 使用JdbcTemplate获取和发送数百万条记录的最佳实践 - Best practice to fetch and send the million of records with JdbcTemplate 结果集向Java模型的转换速度慢,一百万条记录 - Conversion of resultset to Java model slow for a million records 如何将包含 1000 万条记录的巨大结果集转换为 Java 中的列表? - How can I convert a huge Resultset of 10 million records to a list in java? 如何使用java处理1亿条记录插入到db2表中 - How to handle 100 million records inserts into a db2 table using java 如何在Java 1.7上运行JDiff - How to run JDiff on java 1.7 使用Java从Map中的MySQL表中检索百万条记录 - Retrieve million records from MySQL Table in Map using Java 使用MySQL中的100万条记录创建报表并显示在Java JSP页面中 - Creating report from 1 million + records in MySQL and display in Java JSP page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM