最近发现berkeleydb还是不错的,存储相同的数据量,要比磁盘索引小得多,第一次用这样的非关系型数据库,写了个 基本操作的代码
package com.berkeleydb.java;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.je.Cursor;
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;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
public class CRUD {
private File dbPath = new File("D:\\berkeleydb-java2");
@Before
public void init()throws IOException{
/*if(dbPath.exists()){
FileUtils.cleanDirectory(dbPath);
}else{*/
FileUtils.forceMkdir(dbPath);
//}
}
/**
* 向berkeleydb中添加多条信息
* @throws Exception
*/
@Test
public void addObject() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setLocking(true);
//日志的最大长度,日志和数据是一起的,这个参数决定当达到LOG_FILE_MAX后,再切分一个单独的文件
envConfig.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, "104857600");
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
Environment env = new Environment(dbPath, envConfig);
Transaction txn = env.beginTransaction(null, null);
//第一个数据库
Database bindingsDb = env.openDatabase(txn, "bindingsDb", dbConfig);
//这个是用来存储 class信息的库
Database catalogDb = env.openDatabase(txn, "catalogDb", dbConfig);
//绑定可序列化的类
StoredClassCatalog classCatalog = new StoredClassCatalog(catalogDb);
EntryBinding<Document> dataBinding = new SerialBinding<Document>(classCatalog, Document.class);
txn.commit();
DatabaseEntry theKey = new DatabaseEntry();
DatabaseEntry theData = new DatabaseEntry();
for (int i = 0; i < 100; i++) {
Document doc = new Document();
doc.setName("muxiaolin" + i);
doc.setPubtime(System.currentTimeMillis());
doc.setPath("c:\\xiaolin");
StringBinding.stringToEntry(i + "", theKey);
dataBinding.objectToEntry(doc, theData);
//开启事物
txn = env.beginTransaction(null, null);
bindingsDb.put(txn, theKey, theData);
txn.commit();
}
catalogDb.close();
bindingsDb.close();
env.close();
}
@Test
public void delete() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setLocking(true);
envConfig.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, "104857600");
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
Environment env = new Environment(dbPath, envConfig);
Transaction txn = env.beginTransaction(null, null);
Database dataBase = env.openDatabase(txn, "bindingsDb", dbConfig);
String aKey = "20";
DatabaseEntry theKey = new DatabaseEntry();
StringBinding.stringToEntry(aKey, theKey);
if (dataBase.delete(txn, theKey) == OperationStatus.SUCCESS) {
System.out.println("delete success!");
} else {
System.out.println("No record found for key '" + aKey + "'.");
}
txn.commit();
dataBase.close();
env.close();
}
@Test
public void getObject() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setReadOnly(true);
dbConfig.setReadOnly(true);
Environment env = new Environment(dbPath, envConfig);
Database bindingsDb = env.openDatabase(null, "bindingsDb", dbConfig);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
Database catalogDb = env.openDatabase(null, "catalogDb", dbConfig);
StoredClassCatalog classCatalog = new StoredClassCatalog(catalogDb);
EntryBinding<Document> dataBinding = new SerialBinding<Document>(classCatalog, Document.class);
String aKey = "20";
StringBinding.stringToEntry(aKey, foundKey);
if (bindingsDb.get(null, foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
Document doc = dataBinding.entryToObject(foundData);
System.out.println(doc.toString());
} else {
System.out.println("No record found for key '" + aKey + "'.");
}
bindingsDb.close();
catalogDb.close();
env.close();
}
/**
* 游标查询
* @throws Exception
*/
@Test
public void cursor() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setReadOnly(true);
dbConfig.setReadOnly(true);
Environment env = new Environment(dbPath, envConfig);
Database bindingsDb = env.openDatabase(null, "bindingsDb", dbConfig);
Cursor cursor = bindingsDb.openCursor(null, null);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
Database catalogDb = env.openDatabase(null, "catalogDb", dbConfig);
StoredClassCatalog classCatalog = new StoredClassCatalog(catalogDb);
EntryBinding<Document> dataBinding = new SerialBinding<Document>(classCatalog, Document.class);
while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
System.out.println(StringBinding.entryToString(foundKey));
System.out.println(dataBinding.entryToObject(foundData).toString());
}
cursor.close();
bindingsDb.close();
catalogDb.close();
env.close();
}
}
package com.berkeleydb.java;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.je.Cursor;
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;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
public class CRUD {
private File dbPath = new File("D:\\berkeleydb-java2");
@Before
public void init()throws IOException{
/*if(dbPath.exists()){
FileUtils.cleanDirectory(dbPath);
}else{*/
FileUtils.forceMkdir(dbPath);
//}
}
/**
* 向berkeleydb中添加多条信息
* @throws Exception
*/
@Test
public void addObject() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setLocking(true);
//日志的最大长度,日志和数据是一起的,这个参数决定当达到LOG_FILE_MAX后,再切分一个单独的文件
envConfig.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, "104857600");
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
Environment env = new Environment(dbPath, envConfig);
Transaction txn = env.beginTransaction(null, null);
//第一个数据库
Database bindingsDb = env.openDatabase(txn, "bindingsDb", dbConfig);
//这个是用来存储 class信息的库
Database catalogDb = env.openDatabase(txn, "catalogDb", dbConfig);
//绑定可序列化的类
StoredClassCatalog classCatalog = new StoredClassCatalog(catalogDb);
EntryBinding<Document> dataBinding = new SerialBinding<Document>(classCatalog, Document.class);
txn.commit();
DatabaseEntry theKey = new DatabaseEntry();
DatabaseEntry theData = new DatabaseEntry();
for (int i = 0; i < 100; i++) {
Document doc = new Document();
doc.setName("muxiaolin" + i);
doc.setPubtime(System.currentTimeMillis());
doc.setPath("c:\\xiaolin");
StringBinding.stringToEntry(i + "", theKey);
dataBinding.objectToEntry(doc, theData);
//开启事物
txn = env.beginTransaction(null, null);
bindingsDb.put(txn, theKey, theData);
txn.commit();
}
catalogDb.close();
bindingsDb.close();
env.close();
}
@Test
public void delete() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setLocking(true);
envConfig.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, "104857600");
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
Environment env = new Environment(dbPath, envConfig);
Transaction txn = env.beginTransaction(null, null);
Database dataBase = env.openDatabase(txn, "bindingsDb", dbConfig);
String aKey = "20";
DatabaseEntry theKey = new DatabaseEntry();
StringBinding.stringToEntry(aKey, theKey);
if (dataBase.delete(txn, theKey) == OperationStatus.SUCCESS) {
System.out.println("delete success!");
} else {
System.out.println("No record found for key '" + aKey + "'.");
}
txn.commit();
dataBase.close();
env.close();
}
@Test
public void getObject() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setReadOnly(true);
dbConfig.setReadOnly(true);
Environment env = new Environment(dbPath, envConfig);
Database bindingsDb = env.openDatabase(null, "bindingsDb", dbConfig);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
Database catalogDb = env.openDatabase(null, "catalogDb", dbConfig);
StoredClassCatalog classCatalog = new StoredClassCatalog(catalogDb);
EntryBinding<Document> dataBinding = new SerialBinding<Document>(classCatalog, Document.class);
String aKey = "20";
StringBinding.stringToEntry(aKey, foundKey);
if (bindingsDb.get(null, foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
Document doc = dataBinding.entryToObject(foundData);
System.out.println(doc.toString());
} else {
System.out.println("No record found for key '" + aKey + "'.");
}
bindingsDb.close();
catalogDb.close();
env.close();
}
/**
* 游标查询
* @throws Exception
*/
@Test
public void cursor() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
DatabaseConfig dbConfig = new DatabaseConfig();
envConfig.setReadOnly(true);
dbConfig.setReadOnly(true);
Environment env = new Environment(dbPath, envConfig);
Database bindingsDb = env.openDatabase(null, "bindingsDb", dbConfig);
Cursor cursor = bindingsDb.openCursor(null, null);
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
Database catalogDb = env.openDatabase(null, "catalogDb", dbConfig);
StoredClassCatalog classCatalog = new StoredClassCatalog(catalogDb);
EntryBinding<Document> dataBinding = new SerialBinding<Document>(classCatalog, Document.class);
while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
System.out.println(StringBinding.entryToString(foundKey));
System.out.println(dataBinding.entryToObject(foundData).toString());
}
cursor.close();
bindingsDb.close();
catalogDb.close();
env.close();
}
}