我在MySQL数据库中有一个非常大的表,在table中有2亿条记录Users。
我使用JDBC进行查询:
public List<Pair<Long, String>> getUsersAll() throws SQLException {
Connection cnn = null;
CallableStatement cs = null;
ResultSet rs = null;
final List<Pair<Long, String>> res = new ArrayList<>();
try {
cnn = dataSource.getConnection();
cs = cnn.prepareCall("select UserPropertyKindId, login from TEST.users;");
rs = cs.executeQuery();
while (rs.next()) {
res.add(new ImmutablePair<>(rs.getLong(1), rs.getString(2)));
}
return res;
} catch (SQLException ex) {
throw ex;
} finally {
DbUtils.closeQuietly(cnn, cs, rs);
}
}
接下来,我处理结果:
List<Pair<Long, String>> users= dao.getUsersAll();
if (CollectionUtils.isNotEmpty(users)) {
for (List<Pair<Long, String>> partition : Lists.partition(users, 2000)) {
InconsistsUsers.InconsistsUsersCallable callable = new InconsistsUsers.InconsistsUsersCallable (new ArrayList<>(partition));
processExecutor.submit(callable);
}
}
但是由于该表非常大,并且全部都已卸载到内存中,因此我的应用程序崩溃并出现错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
从服务器成功接收到的最后一个数据包是105619毫秒之前。
如何才能部分接收数据并按优先级顺序处理它们,以免一次将所有结果上传到内存中?创建游标并将数据上传到非阻塞队列并在数据到达时对其进行处理是可能的。如何才能做到这一点?
芜湖不芜
慕田峪9158850
DIEA
相关分类