在集群上支持数据库大数据量导出

83 篇文章 1 订阅
13 篇文章 0 订阅

80w行的数据导出

数据库表(经过程序处理)导出一般使用EXCEL文件,技术一般有POIJXLFastExcel。但是当文件过大(几十个字段,行数超过200,000)的时候,往往会出现内存溢出OutOfMemery,这个是应用无非承受的。

对于导出这么大量的数据,一般没有实时性的要求,也没有外观的要求。

业务部分的需求一般是:导出80W行数据,不管是什么,最后能变化成EXCEL即可。

解决方案:直接导出成TXT的格式,再由EXCEL打开,这样一般能满足业务的需求。

涉及的技术,压缩、并发等技术还要跟集群结合。

压缩:直接将txt压缩成RAR文件,没有什么难度。

并发:我们在后台起一个线程或者几个线程来运行一段程序,操作数据的任务表。(可以是单台机器操作)。

任务处理:直接在数据库建立任务表。

 

基本的结构如下所示:

交互图

  1. 用户的一个下载请求,传到这个服务器,服务器再传到第一台服务器。
  2. 第一台服务器向DB的任务表中插入一个条任务信息。
  3. 当第一台服务器启动的时候,启动一个线程,定时去查看DB中有没有任务。
  4. 如果有任务,则 处理,从底层BO中取得需要的数据。
  5. 直接写入文件file.txt中,再将压缩成file.rar文件
  6. 在页面展示一个下载中心,用户可以下载文件、也可以删除文件,甚至可以查看当然的下载任务的处理剩余时间。

 

file.rar必须挂在存储上,多台服务器共享。

 

 压缩源码(核心部分):

public static final String FILE_STYLE = ".rar";<br />

public static final String CODE = "ISO8859_1";<br />

public static BigDecimal compressedFiles(File file) throws IOException {<br />

File f = new File(file.getPath() + FILE_STYLE);<br />

compress(file, f);<br />

file.delete();<br />

return new BigDecimal(f.length() / (1024 * 1024 * 1.0));<br />

}<br />

public static void compress(File file, File ff) throws IOException {<br />

FileOutputStream f = new FileOutputStream(ff);<br />

CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());<br />

ZipOutputStream out = new ZipOutputStream(<br />

new BufferedOutputStream(csum));<br />

out.setComment("压缩的文件,文件是txt的");<br />

BufferedReader in = new BufferedReader(new InputStreamReader(<br />

new FileInputStream(file), CODE));<br />

out.putNextEntry(new ZipEntry(file.getName()));<br />

int c;<br />

while ((c = in.read()) != -1)<br />

out.write(c);<br />

out.flush();<br />

out.close();<br />

in.close();<br />

f.close();<br />

csum.close();<br />

}

NotepadRender:

 

public static final String FILE_STYLE = ".rar";<br />

public static final String CODE = "ISO8859_1";<br />

public static BigDecimal compressedFiles(File file) throws IOException {<br />

File f = new File(file.getPath() + FILE_STYLE);<br />

compress(file, f);<br />

file.delete();<br />

return new BigDecimal(f.length() / (1024 * 1024 * 1.0));<br />

}<br />

public static void compress(File file, File ff) throws IOException {<br />

FileOutputStream f = new FileOutputStream(ff);<br />

CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());<br />

ZipOutputStream out = new ZipOutputStream(<br />

new BufferedOutputStream(csum));<br />

out.setComment("压缩的文件,文件是txt的");<br />

BufferedReader in = new BufferedReader(new InputStreamReader(<br />

new FileInputStream(file), CODE));<br />

out.putNextEntry(new ZipEntry(file.getName()));<br />

int c;<br />

while ((c = in.read()) != -1)<br />

out.write(c);<br />

out.flush();<br />

out.close();<br />

in.close();<br />

f.close();<br />

csum.close();<br />

 

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值