[英]How to Read Large data from database using java?
I am having more then 2gb data in my table i need to read more the 1gb data from the single table, i know various option available in db side to achieve this but i need better approach in java code, can any one tell with example java code like parallel processing in multi threading.我的表中有超过 2gb 的数据我需要从单个表中读取更多的 1gb 数据,我知道 db 端可用的各种选项来实现这一点,但我需要在 Java 代码中使用更好的方法,任何人都可以通过示例 java 告诉我多线程中类似并行处理的代码。
example Code示例代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SelectRowsExample {
public static void main(String[] args) {
Connection connection = null;
try {
// Load the MySQL JDBC driver
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String serverName = "localhost";
String schema = "test";
String url = "jdbc:mysql://" + serverName + "/" + schema;
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
System.out.println("Successfully Connected to the database!");
} catch (ClassNotFoundException e) {
System.out.println("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
System.out.println("Could not connect to the database " + e.getMessage());
}
try {
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM employee orderby dept");
while (results.next()) {
String empname = results.getString("name");
System.out.println("Fetching data by column index for row " + results.getRow() + " : " + empname);
String department = results.getString("department");
System.out.println("Fetching data by column name for row " + results.getRow() + " : " + department);
}
} catch (SQLException e) {
System.out.println("Could not retrieve data from the database " + e.getMessage());
}
}
}
Here my query will return name and department details more the 1gb data will come for each department.在这里,我的查询将返回名称和部门详细信息,每个部门都会有更多的 1GB 数据。 if i use this way it will surly slow down the application.
如果我使用这种方式,它肯定会减慢应用程序的速度。 that's why i thought go for parallel processing in multithreading.
这就是为什么我想在多线程中进行并行处理。 any one kindly give me the suggestion to read the huge amount of data quickly.
任何人都请给我建议快速阅读大量数据。
In your example you don't have to use high calibre gun like paralellism.在您的示例中,您不必像并行那样使用大口径枪。 Also it doesn't necessarily solves your problem because there could be a lot of bottlenecks because of hardware, network, etc as luk2302 mentioned it.
此外,它不一定能解决您的问题,因为正如 luk2302 提到的那样,由于硬件、网络等可能存在很多瓶颈。
There are two much easier tweaks:有两个更容易的调整:
ResultSet results = statement.executeQuery("SELECT name, department FROM employee orderby dept");
Statement statement = connection.createStatement();
statement.setFetchSize(1000);
if(results.getRow()%1000 == 0) {
System.out.println("Fetching data by column index for row " + results.getRow() + " : " + empname);
}
Br, Nandor Br, 南多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.