添加相关依赖
<!-- Berkeley DB Java Edition 如果是5以上的版本,则需要添加oracle的仓库 -->
<repositories>
<repository>
<id>oracleReleases</id>
<name>Oracle Released Java Packages</name>
<url>http://download.oracle.com/maven</url>
<layout>default</layout>
</repository>
</repositories>
编写读写工具类
package util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.hoyi.DB.ctrl.Console;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.CursorConfig;
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.LockConflictException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.TransactionConfig;
import sun.security.jgss.LoginConfigImpl;
public class BerkeleyDBUtil {
// 数据库环境
private Environment env = null;
// 数据库
private static Database berkeleyDatabase = null;
// 数据库名
private static String dbName = "berkeley_Database";
public BerkeleyDBUtil(String homeDirectory) {
// 1、创建EnvironmentConfig
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setCacheSize(100 * 1024 * 1024);
// 2、使用EnvironmentConfig配置Environment
env = new Environment(new File(homeDirectory), envConfig);
// 3、创建DatabaseConfig
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
// 4、使用Environment与DatabaseConfig打开Database
berkeleyDatabase = env.openDatabase(null, dbName, dbConfig);
}
/*
* 向数据库中写入记录,并判断是否可以有重复数据。 传入key和value
* 若可以有重复数据,则直接使用put()即可,若不能有重复数据,则使用putNoOverwrite()。
*/
public boolean writeToDatabase(String key, String value, boolean isOverwrite) {
try {
// 设置key/value,注意DatabaseEntry内使用的是bytes数组
DatabaseEntry Key = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry Value = new DatabaseEntry(value.getBytes("UTF-8"));
OperationStatus status = null;
Transaction txn = null;
try {
// 1、Transaction配置
TransactionConfig txConfig = new TransactionConfig();
txConfig.setSerializableIsolation(true);
txn = env.beginTransaction(null, txConfig);
// 2、写入数据
if (isOverwrite) {
status = berkeleyDatabase.put(txn, Key, Value);
} else {
status = berkeleyDatabase.putNoOverwrite(txn, Key, Value);
}
txn.commit();
if (status == OperationStatus.SUCCESS) {
System.out.println("向Berkeley数据库" + dbName + "中写入:" + key + "," + value);
return true;
} else if (status == OperationStatus.KEYEXIST) {
System.out.println("向Berkeley数据库" + dbName + "中写入:" + key + "," + value + "失败,该值已经存在");
return false;
} else {
System.out.println("向Berkeley数据库" + dbName + "中写入:" + key + "," + value + "失败");
return false;
}
} catch (LockConflictException lockConflict) {
txn.abort();
System.out.println("向Berkeley数据库" + dbName + "中写入:" + key + "," + value + "出现lock异常");
return false;
}
} catch (Exception e) {
// 错误处理
System.out.println("向Berkeley数据库" + dbName + "中写入:" + key + "," + value + "出现错误");
return false;
}
}
/*
* 从数据库中读出数据 传入key 返回value
*/
public String readFromDatabase(String key) {
try {
DatabaseEntry Key = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry Value = new DatabaseEntry();
Transaction txn = null;
try {
// 1、配置 Transaction相关信息
TransactionConfig txConfig = new TransactionConfig();
txConfig.setSerializableIsolation(true);
txn = env.beginTransaction(null, txConfig);
// 2、读取数据
OperationStatus status = berkeleyDatabase.get(txn, Key, Value, LockMode.DEFAULT);
txn.commit();
if (status == OperationStatus.SUCCESS) {
// 3、将字节转换成String
byte[] retData = Value.getData();
String value = new String(retData, "UTF-8");
System.out.println("从Berkeley数据库" + dbName + "中读取:" + key + "," + value);
return value;
} else {
System.out.println("No record found for key '" + key + "'.");
return StringUtils.EMPTY;
}
} catch (LockConflictException lockConflict) {
txn.abort();
System.out.println("从Berkeley数据库" + dbName + "中读取:" + key + "出现lock异常");
return StringUtils.EMPTY;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return StringUtils.EMPTY;
}
}
/*
* 遍历数据库中的所有记录,返回list
*/
public ArrayList<String> getEveryItem() {
// TODO Auto-generated method stub
System.out.println("===========遍历数据库" + dbName + "中的所有数据==========");
Cursor myCursor = null;
ArrayList<String> resultList = new ArrayList<String>();
Transaction txn = null;
try {
txn = this.env.beginTransaction(null, null);
CursorConfig cc = new CursorConfig();
cc.setReadCommitted(true);
if (myCursor == null)
myCursor = berkeleyDatabase.openCursor(txn, cc);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
// 使用cursor.getPrev方法来遍历游标获取数据
if (myCursor.getFirst(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
String theKey = new String(foundKey.getData(), "UTF-8");
String theData = new String(foundData.getData(), "UTF-8");
resultList.add(theKey);
System.out.println("Key | Data : " + theKey + " | " + theData + "");
while (myCursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
theKey = new String(foundKey.getData(), "UTF-8");
theData = new String(foundData.getData(), "UTF-8");
resultList.add(theKey);
System.out.println("Key | Data : " + theKey + " | " + theData + "");
}
}
myCursor.close();
txn.commit();
return resultList;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
System.out.println("getEveryItem处理出现异常");
txn.abort();
if (myCursor != null) {
myCursor.close();
}
return null;
}
}
/*
* 根据key值删除数据库中的一条记录
*/
public boolean deleteFromDatabase(String key) {
boolean success = false;
long sleepMillis = 0;
for (int i = 0; i < 3; i++) {
if (sleepMillis != 0) {
try {
Thread.sleep(sleepMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
sleepMillis = 0;
}
Transaction txn = null;
try {
// 1、使用cursor.getPrev方法来遍历游标获取数据
TransactionConfig txConfig = new TransactionConfig();
txConfig.setSerializableIsolation(true);
txn = env.beginTransaction(null, txConfig);
DatabaseEntry theKey;
theKey = new DatabaseEntry(key.getBytes("UTF-8"));
// 2、删除数据 并提交
OperationStatus res = berkeleyDatabase.delete(txn, theKey);
txn.commit();
if (res == OperationStatus.SUCCESS) {
System.out.println("从Berkeley数据库" + dbName + "中删除:" + key);
success = true;
return success;
} else if (res == OperationStatus.KEYEMPTY) {
System.out.println("没有从Berkeley数据库" + dbName + "中找到:" + key + "。无法删除");
} else {
System.out.println("删除操作失败,由于" + res.toString());
}
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (LockConflictException lockConflict) {
System.out.println("删除操作失败,出现lockConflict异常");
sleepMillis = 1000;
continue;
} finally {
if (!success) {
if (txn != null) {
txn.abort();
}
}
}
}
return false;
}
public void closeDB() {
if (berkeleyDatabase != null) {
berkeleyDatabase.close();
System.out.println("===========关闭Database==========");
}
if (env != null) {
env.close();
System.out.println("===========关闭Environment==========");
}
}
}
测试方法
package WebRoot.controller;
import java.io.File;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.hoyi.dishop.Hoyipage;
import org.hoyi.wb.comment.RequestMode;
import org.hoyi.wb.comment.RequestType;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import util.BerkeleyDBUtil;
import util.MongodbUtil;
@RequestMode(MODE= {RequestType.GET,RequestType.POST})
public class BerkeleyDBTest extends Hoyipage{
private static BerkeleyDBUtil dbUtil = null;
@RequestMode(MODE= {RequestType.GET, RequestType.POST})
public void BerkeleyReadAndWrite() {
try {
String filePath = "D:\\berkeley";
File file = new File(filePath);
if(!file.exists()) {
file.mkdirs();
}
dbUtil = new BerkeleyDBUtil(filePath);
MongoCollection<Document> collection = MongodbUtil.getCollection("discovery");
MongoCursor<Document> cursor = collection.find().iterator();
JSONArray jsonArray = new JSONArray();
while(cursor.hasNext()) {
Document resultDoc = cursor.next();
jsonArray.add(resultDoc);
}
boolean flag = dbUtil.writeToDatabase("discovery", jsonArray.toString(), false);
if(flag) {
System.out.println("数据插入成功");
}else {
System.out.println("数据插入失败");
}
String jsonStr = dbUtil.readFromDatabase("discovery");
if(StringUtils.isBlank(jsonStr)) {
System.out.println("No key found for 'discovery");
return;
}else {
JSONArray Array = JSONArray.fromObject(jsonStr);
JSONObject jsonObject = new JSONObject();
JSONArray resultArray = new JSONArray();
for(int i=0;i<Array.size();i++) {
jsonObject = JSONObject.fromObject(Array.get(i));
jsonObject.remove("_id");
resultArray.add(jsonObject);
}
if(resultArray.size() > 0) {
this.WriteUTF8JSONDATAMSG(1, "success", resultArray.toArray());
}
}
boolean deleteflag = dbUtil.deleteFromDatabase("discovery");
if(deleteflag) {
System.out.println("数据删除成功");
}else {
System.out.println("数据删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(dbUtil != null) {
dbUtil.closeDB();
}
}
}
}