Java对数据库的字符串字段进行压缩保存
来源:互联网 发布:淘宝网购物皮衣 编辑:程序博客网 时间:2024/05/18 08:42
核心提示:数据字段一般都是保存原文的,一来方便在数据库修改和维护,而来有一些查询要用到它。但是在有些时候,我们无需保存原文了,比如在论坛,博客等数据里的内容字段,一般使用Clob类型,其很少参与搜索,而且就算要全文检索,我们也不推荐使用数据库的like 等,
数据字段一般都是保存原文的,一来方便在数据库修改和维护,而来有一些查询要用到它。但是在有些时候,我们无需保存原文了,比如在论坛,博客等数据里的内容字段,一般使用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;
- }
- }
- }
- 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;
- } else {
- // 尝试编码
- this.content = StringZip.zipString(content);
- // 如果编码后的数据更长
- if (this.content.length() > content.length()) {
- this.content = content;
- ziped = false;
- } else {
- ziped = true;
- }
- }
- }
- 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;
- } else {
- // 尝试编码
- this.content = StringZip.zipString(content);
- // 如果编码后的数据更长
- if (this.content.length() > content.length()) {
- this.content = content;
- ziped = false;
- } else {
- ziped = true;
- }
- }
- }
增加了一个是否压缩的属性。
在读取时,根据是否压缩的标志进行操作,在设置时,根据数据长度和压缩的效果更新数据,并设置压缩标志。
通过这个,数据将被压缩保存,一般的文本压缩率还是很高的,到底有多高,你自己测试看看就知道了。
- Java对数据库的字符串字段进行压缩保存
- Java对数据库的字符串字段进行压缩保存【当字符串大于4000字符】
- Java对数据库的字符串字段进行压缩保存的尝试
- Java对字符串进行压缩
- JAVA对字符串进行压缩编码
- 使用java.util.zip对字符串进行压缩和解压缩
- 使用java.util.zip对字符串进行压缩和解压缩
- C#对指定的文件调用RAR进行压缩保存
- [Java]使用gzip对字符串进行压缩/解压
- Java对字符串的压缩与解压
- JAVA对字符串的压缩与解压缩
- Java对字符串的压缩与解压缩
- 使用函数分组对某个字段进行字符串的累加
- 对字符串型日期字段进行排序的体会
- 根据字段02对字符串进行分行
- PHP 对字符串进行压缩和解压
- Java对字符串进行的操作
- 对输入字符串进行压缩,输入"aaabcccdde",输出"3ab3c2de",即对连续出现的字符进行压缩。
- svn update 目录包含中文时出错(svn:Can’t convert string from ‘UTF-8′ to native encoding)
- Porting cgic to arm linux
- AAA服务器
- android Toast 自定义样式
- c++实现链表反转
- Java对数据库的字符串字段进行压缩保存
- Porting thttpd web server to ARM-Linux
- Android overlay 学习 二 Android camera preview and take picture with V4l2
- Linux 编程之Bash中的变量
- 平台调用P-INVOKE(一)--(基础篇)
- 详细从浅到深讲解委托和事件
- VC 界面库皮肤库
- Synergy – 用一套键盘/鼠标控制多台电脑
- Porting Lua to ARM