我需要查询数据库并将结果集复制到另一个数据库中,该数据库具有相同的数据库结构并且也位于同一主机系统上。
如果查询结果非常小,以下JAVA函数运行良好(快速且无错误):
public void copyTableData(Connection dbConnOnSrcDB, Connection dbConnOnDestDB,
String sqlQueryOnSrcDB, String tableNameOnDestDB)
throws SQLException {
try (
PreparedStatement prepSqlStatmOnSrcDB = dbConnOnSrcDB.prepareStatement(sqlQueryOnSrcDB);
ResultSet sqlResultsFromSrcDB = prepSqlStatmOnSrcDB.executeQuery()
) {
ResultSetMetaData sqlMetaResults = sqlResultsFromSrcDB.getMetaData();
// Stores the query results
List<String> columnsOfQuery = new ArrayList<>();
// Store query results
for (int i = 1; i <= sqlMetaResults.getColumnCount(); i++)
columnsOfQuery.add(sqlMetaResults.getColumnName(i));
try (
PreparedStatement prepSqlStatmOnDestDB = dbConnOnDestDB.prepareStatement(
"INSERT INTO " + tableNameOnDestDB +
" (" + columnsOfQuery.stream().collect(Collectors.joining(", ")) + ") " +
"VALUES (" + columnsOfQuery.stream().map(c -> "?").collect(Collectors.joining(", ")) + ")")
) {
while (sqlResultsFromSrcDB.next()) {
for (int i = 1; i <= sqlMetaResults.getColumnCount(); i++)
prepSqlStatmOnDestDB.setObject(i, sqlResultsFromSrcDB.getObject(i));
prepSqlStatmOnDestDB.addBatch();
}
prepSqlStatmOnDestDB.executeBatch();
}
}
}
但是我有非常大的数据库查询和数百万兆字节范围内的结果集。
问题A:当处理完第二行代码时,我发现下面的OutOfMemoryError正在提升:
ResultSet sqlResultsFromSrcDB = prepSqlStatmOnSrcDB.executeQuery()
JAVA-Exeption:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
at java.lang.Class.getDeclaredField(Class.java:2068)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl$1.run(AtomicReferenceFieldUpdater.java:323)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl$1.run(AtomicReferenceFieldUpdater.java:321)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(AtomicReferenceFieldUpdater.java:320)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdater.java:110)
at java.sql.SQLException.<clinit>(SQLException.java:372)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2156)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:117)
at Application.copyTableData(Application.java:159)
at Application.main(Application.java:585)
问题B:复制工作需要很多时间。有没有办法加快复制过程?
数据库查询是:
String[] tables = new String[]{
"table1",
"table1_properties",
"table1_addresses",
"table2",
"table3",
"table4",
"table5",
"table6",
"table7",
"table8",
"table9",
"table10"
};
函数调用:
for( String table : tables ){
getDataFromSrcDB = "SELECT " + table + ".* " +
"FROM table1 " +
"FULL JOIN table1_properties " +
"ON table1_properties.d_id=table1.d_id " +
"FULL JOIN table1_addresses " +
"ON table1_addresses.d_id=table1_properties.d_id " +
"FULL JOIN table2 " +
"ON table2.p_id=table1_properties.p_id " +
"FULL JOIN table3 " +
"ON table3.d_id=table1.d_id " +
"FULL JOIN table4 " +
"ON table4.d_id=table1.d_id " +
"FULL JOIN table5 " +
"ON table5.d_id=table1.d_id " +
"FULL JOIN table6 " +
"ON table6.d_id=table1.d_id " +
"FULL JOIN table7 " +
"ON table7.d_id=table1.d_id " +
"FULL JOIN table8 " +
"ON table8.id=table4.id " +
"FULL JOIN table9 " +
"ON table9.d_id=table1.d_id " +
"FULL JOIN table10 " +
"ON table10.a_id=table1_addresses.a_id " +
"WHERE ST_Intersects(ST_MakeEnvelope(" +
minLong + "," +
minLat + "," +
maxLong + "," +
maxLat + ",4326), geom :: GEOMETRY) OR " +
"ST_Intersects(ST_MakeEnvelope(" +
minLong + "," +
minLat + "," +
maxLong + "," +
maxLat + ",4326), CAST(table3.location AS GEOMETRY))";
copyTableData(dbConnOnSrcDB, dbConnOnDestDB, getDataFromSrcDB, table);
}
答案 0 :(得分:4)
当批量的大小很大时,会出现此错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
我有一些解决方案。
相反,您可以将批次划分为小批量,例如每个1_000持久保存数据,您还需要一些配置,如注释中提及Mark Rotteveel,并且文档中提到Getting results based on a cursor:
默认情况下,驱动程序会立即收集查询的所有结果。 对于大型数据集这样的JDBC驱动程序可能不方便 提供了一种仅在数据库游标上建立ResultSet的方法 获取少量行。
那么你应该做什么:
在这种情况下,您的代码可以是这样的:
//Note here you set auto commit for the source connection
dbConnOnSrcDB.setAutoCommit(false);
final int batchSize = 1000;
final int fetchSize = 50;
int count = 0;
...
//Set the appropriate size for the FetchSize
sqlResultsFromSrcDB.setFetchSize(fetchSize);
while (sqlResultsFromSrcDB.next()) {
for (int i = 1; i <= sqlMetaResults.getColumnCount(); i++) {
prepSqlStatmOnDestDB.setObject(i, sqlResultsFromSrcDB.getObject(i));
}
prepSqlStatmOnDestDB.addBatch();
if (++count % batchSize == 0) {
prepSqlStatmOnDestDB.executeBatch();
}
}
prepSqlStatmOnDestDB.executeBatch(); // insert remaining records
因为您正在使用PostgreSQL,所以我想使用dblink在数据库之间将数据传输到另一个数据库。
一些有用的链接:
答案 1 :(得分:0)
你有很多方法可以实现它。您可以选择以下几个选项 -
从First数据库读取数据并将其写入csv文件,然后从块中的csv文件再次读取并写入另一个数据库。(易于实现但更多编码) https://gauravmutreja.wordpress.com/2011/10/13/exporting-your-database-to-csv-file-in-java/
如果在将数据传输到另一个数据库之前没有太多的数据操作,您可以编写一个简单的DB函数来从一个数据库中读取数据并写入另一个数据库。
答案 2 :(得分:0)
当您直接获取数据时,它会填充 ram,直到获取所有数据。因此您很容易看到 OutOfMemoryError
错误。
如果您使用 stream
获取数据,您可以捕获无限数据,因为 stream
获取、处理和继续处理新数据,清除已处理数据的 ram(与 fetchSize
分开) )