Java对数据库的字符串字段进行压缩保存的尝试

    数据字段一般都是保存原文的,一来方便在数据库修改和维护,而来有一些查询要用到它。但是在有些时候,我们无需保存原文了,比如在论坛,博客等数据里的内容字段,一般使用Clob类型,其很少参与搜索,而且就算要全文检索,我们也不推荐使用数据库的like 等,而应该用第三方的全文检索工具,比如lucene等实现。

    这类数据都是大量的文本数据,有很大的可压缩性。由于一些原因,我的数据库已经超过我能容忍的大小了,所以想到了是否可以把这个数据压缩存储来节省空间,于是有了如下的尝试。

    压缩算法就先不过多考虑了,就用Zip进行尝试就可以了。先看看如何把字符串压缩和解压缩的算法。


package com.laozizhu.article.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * 把字符串使用ZIP压缩和解压缩的代码。 * * @author JAVA世纪网(java2000.net, laozizhu.com) */ public class StringZip { public static String zipString(String str) { try { ByteArrayOutputStream bos = null; GZIPOutputStream os = null; byte[] bs = null; try { bos = new ByteArrayOutputStream(); os = new GZIPOutputStream(bos); os.write(str.getBytes()); os.close(); bos.close(); bs = bos.toByteArray(); return new String(bs, "iso-8859-1"); } finally { bs = null; bos = null; os = null; } } catch (Exception ex) { return str; } } public static String unzipString(String str) { ByteArrayInputStream bis = null; ByteArrayOutputStream bos = null; GZIPInputStream is = null; byte[] buf = null; try { bis = new ByteArrayInputStream(str.getBytes("ISO-8859-1")); bos = new ByteArrayOutputStream(); is = new GZIPInputStream(bis); buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1) { bos.write(buf, 0, len); } is.close(); bis.close(); bos.close(); return new String(bos.toByteArray()); } catch (Exception ex) { return str; } finally { bis = null; bos = null; is = null; buf = null; } } }


然后就是如何压缩和解压缩,应该放在哪里的问题。我考虑了一下,发现JavaBean这东西真的有趣,竟然可以实现透明压缩和解压缩。看代码:


private String content; // 增加一个是否压缩的字段,没办法,有些字段压缩了反到更长了 private boolean ziped; public boolean isZiped() { return ziped; } public void setZiped(boolean ziped) { this.ziped = ziped; } /** * 读取内容。 * @return the content */ public String getContent() { // 解码 if (isZiped()) { return StringZip.unzipString(content); } return content; } /** * 设置新的内容 * @param content the content to set */ public void setContent(String content) { if (content == null || content.length() < 512) { this.content = content; ziped = false; } else { // 尝试编码 this.content = StringZip.zipString(content); // 如果编码后的数据更长 if (this.content.length() > content.length()) { this.content = content; ziped = false; } else { ziped = true; } } }


增加了一个是否压缩的属性。

在读取时,根据是否压缩的标志进行操作,在设置时,根据数据长度和压缩的效果更新数据,并设置压缩标志。

通过这个,数据将被压缩保存,一般的文本压缩率还是很高的,到底有多高,你自己测试看看就知道了。

你可能感兴趣的:(java,数据库,exception,String,null,全文检索)