Java将MySQL数据写入csv_使用Java将大量数据从数据库导出到.csv时出...

在Java应用中,使用JPA和TopLink从数据库导出大量(600万行)数据到CSV文件时遇到内存问题。尝试了BufferedWriter、RandomAccessFile和FileChannel等方法,但内存消耗高导致Java堆内存不足异常。当前策略是按行写入并释放内存,但仍然遇到问题。寻求导出大量数据的优化策略。
摘要由CSDN通过智能技术生成

我,谢谢您的关注.

我想使用Java将大量数据,实际上是大量数据(600万行)导出到.csv文件.该应用程序是使用toplink(ojdbc14)的JPA摆动应用程序.

我尝试使用:

但是BufferedWriter

RandomAccessFile的

FileChannel

等等,但是尽管我将最大堆大小设置为800m(-Xmx800m),但是内存消耗仍然很高,导致Java堆内存不足异常.

我的源代码的最新版本:

...(more lines of code)

FileChannel channel = getRandomAccessFile(tempFile).getChannel();

Object[][] data = pag.getRawData(); //Database data in a multidimentional array

for (int j = 0; j < data.length; j++) {

write(data[j], channel); //write data[j] (an array) into the channel

freeStringLine(data[j]); //data[j] is an array, this method sets all positions =null

data[j] = null;//sets reference in null

}

channel.force(false); //force writing in file system (HD)

channel.close(); //Close the channel

pag = null;

...(more lines of code)

private void write(Object[] row, FileChannel channel) throws DatabaseException {

if (byteBuff == null) {

byteBuff = ByteBuffer.allocateDirect(1024 * 1024);

}

for (int j = 0; j < row.length; j++) {

if (j < row.length - 1) {

if (row[j] != null) {

byteBuff.put(row[j].toString().getBytes());

}

byteBuff.put(SPLITER_BYTES);

} else {

if (row[j] != null) {

byteBuff.put(row[j].toString().getBytes());

}

}

}

byteBuff.put("

".toString().getBytes());

byteBuff.flip();

try {

channel.write(byteBuff);

} catch (IOException ex) {

throw new DatabaseException("Imposible escribir en archivo temporal de exportación : " + ex.getMessage(), ex.getCause());

}

byteBuff.clear();

}

作为600万行,我不想在创建文件时将该数据存储在内存中.我制作了许多临时文件(每个文件有5000行),最后,使用两个FileChannel将所有这些临时文件附加到一个文件中.但是,缺少内存的异常是在加入之前启动的.

您现在是否是导出大量数据的另一种策略?

非常感谢您的答复.对不起,我的英文,我正在提高xD

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值