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));
}
}