BerkeleyDB数据库的简介和工具类的JAVA实现

        berkeleydb是一款古老而功能强大嵌入式数据库(伴随代码运行存在),数据插入和查询效率极高,内外存相结合的 kv 数据库、文件型数据库。内存意味着效率极高,外存意味着可持久化。早期MySQL的元数据就使用berkeleyDB存储。BDB跨语言支持l良好,且轻量级可拓展,是不错的非关系型数据库选择,适合构建查询引擎。
       当然他也有缺点:无网络通信模块,数据共享不方便。不支持SQL,现在已支持但用的不多。

       使用BDB数据库需要BDB依赖的JAR包,maven项目添加依赖即可,我就不贴依赖源了,上网直接搜berkeleyDB进官网查看依赖源即可。

package cn.tl.g3.utils;

import java.io.File;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;

/**
 * 封装的bdb操作工具类 集成了增、删、改、查、关闭、同步操作等方法
 */

/**
 * @author zhangxin
 */
/**
 * @author zhangxin
 */
/**
 * @author zhangxin
 */
public class BDBOperatorUtil {
	private String dbEnvFilePath;
	private String databaseName;
	Environment myEnvironment = null;
	private Database weiboDatabase = null;

	public BDBOperatorUtil(String dbEnvFilePath, String databaseName) {
		this.dbEnvFilePath = dbEnvFilePath;
		this.databaseName = databaseName;
		/**
		 * 初始化数据库参数
		 */
		try {
			File file = new File(dbEnvFilePath);
			if (!file.exists()) {
				file.mkdirs();
			}
			EnvironmentConfig envConfig = new EnvironmentConfig();
			envConfig.setAllowCreate(true);
			DatabaseConfig dbConfig = new DatabaseConfig();
			dbConfig.setAllowCreate(true);

			myEnvironment = new Environment(file, envConfig);
			weiboDatabase = myEnvironment.openDatabase(null, databaseName,
					dbConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param key
	 * @param value
	 * @param isSync
	 * @return
	 * 
	 *         将指定的kv对存放到bdb当中,并可以选择是否实时同步到碰盘中
	 */
	public boolean put(String key, String value, boolean isSync) {
		try {
			DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
			DatabaseEntry theValue = new DatabaseEntry(value.getBytes("UTF-8"));

			weiboDatabase.put(null, theKey, theValue);
			if (isSync) {
				this.sync();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @param key
	 *            删除bdb中指定的key值
	 */
	public boolean delete(String key) {
		try {
			DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
			weiboDatabase.delete(null, theKey);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @param key
	 *            读取指定key
	 */
	public String getValue(String key) {
		try {
			DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
			DatabaseEntry theValue = new DatabaseEntry();
			weiboDatabase.get(null, theKey, theValue, LockMode.DEFAULT);
			if (theValue.getData() == null) {
				return null;
			}
			return new String(theValue.getData(), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 同步数据到碰盘当中,相当于让数据操作实时持久化
	 */
	public boolean sync() {
		if (weiboDatabase != null) {
			try {
				weiboDatabase.sync();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * @return boolean 关闭数据库
	 */
	public boolean close() {
		try {
			// // 先关闭数据库
			if (weiboDatabase != null) {
				weiboDatabase.close();
			}
			// // 再关闭BDB系统环境变量
			if (myEnvironment != null) {
				myEnvironment.sync();
				myEnvironment.cleanLog(); // 在关闭环境前清理下日志
				myEnvironment.close();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

	/**
	 * 测试
	 */
	public static void main(String[] args) {
		// 数据库所在的存储文件夹
		String dbEnvFilePath = "bdb2";
		// 数据库名称
		String databaseName = "weibo2";
		String key = "zx";
		String value = "hello";

		// 初始化
		BDBOperatorUtil bdbUtil = new BDBOperatorUtil(dbEnvFilePath,
				databaseName);
		// 增加数据
		// bdbUtil.put(key, value, false);
		// 删除数据
		bdbUtil.delete(key);
		// bdbUtil.sync();
		System.out.println(bdbUtil.getValue(key));

	}
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值