BerkeleyDB .je 在作为存储时的简单使用

2019-04-16 11:53:27 浏览数 (1)

最近在研究BDB时发现速度特别快(非关系型数据库)下面我给大家共享一下我在学习的过程中的一些收获和问题,不知道哪位大神帮忙解决一下。主要在putNoDupData,不知道该怎么使用

代码语言:javascript复制
package com.bdb;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import jodd.io.FileUtil;

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

public class MyBerkeleyDB {
			 
			private Environment env;
			private Database db;
			public MyBerkeleyDB() {
			}
			
			public void setUp(String path, long cacheSize) {
			EnvironmentConfig envConfig = new EnvironmentConfig();
			envConfig.setAllowCreate(true);
			envConfig.setCacheSize(cacheSize);
				try {
				env = new Environment(new File(path),envConfig);
				} catch (DatabaseException e) {
				e.printStackTrace();
				}
			}
			
			public void open(String dbName) {
			DatabaseConfig dbConfig = new DatabaseConfig();
			dbConfig.setAllowCreate(true);//是否允许创建
			dbConfig.setReplicated(false);//是否允许重复
			//dbConfig.setSortedDuplicates(true);这里我已经设置了,我不知道下面在我putNoDupData会出现异常
			try {
			db = env.openDatabase(null, dbName, dbConfig);
			} catch (DatabaseException e) {
			e.printStackTrace();
			}
			}
			
			public void close() {
			try {
			if(db != null) {
			db.close();
			}
			
			if(env != null) {
			env.close();
			}
			} catch (DatabaseException e) {
			e.printStackTrace();
			}
			}
			
			public String get(String key) throws Exception {
			DatabaseEntry queryKey = new DatabaseEntry();
			DatabaseEntry value = new DatabaseEntry();
			queryKey.setData(key.getBytes("UTF-8"));
			
			OperationStatus status = db.get(null, queryKey, value,
			LockMode.DEFAULT);
			if (status == OperationStatus.SUCCESS) {
			return new String(value.getData());
			}
			return null;
			}
			 
			  /**
			   * put data for one to one
			   * @param key
			   * @param value
			   * @return
			   * @throws Exception
			   */
			public boolean put(String key, String value) throws Exception {
			byte[] theKey = key.getBytes("UTF-8");
			byte[] theValue = value.getBytes("UTF-8");
		 
			OperationStatus status = db.put(null, new DatabaseEntry(theKey),new DatabaseEntry(theValue));
			if(status == OperationStatus.SUCCESS) {
			return true;
			}
			return false;
			}
			/**
			 * get all value
			 */
			public static List<String> readFile(String filePathAndName)throws IOException {
				
				FileInputStream fis = new FileInputStream(filePathAndName);
				InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
				BufferedReader br = new BufferedReader(isr);
				LineNumberReader lnr = new LineNumberReader(br);
				
				List<String> returnValue = new ArrayList<String>();
				int cnt = 0;
				while (true) {
					cnt  ;
					String tempStr = lnr.readLine();
					if (tempStr == null)
						break;
					/*if(cnt>1)
						break;*/
					/*if(cnt<=1000000)
						continue;
					if(cnt>1500000)
						break;*/
					if (tempStr.length() < 2)
						continue;
					returnValue.add(tempStr);
				}
				lnr.close();
				br.close();
				isr.close();
				fis.close();
				return returnValue;
				}
			
		  public List<String> getAllValue(){
				List<String> returnValue = new ArrayList<String>();
				DatabaseEntry keyEntry = new DatabaseEntry();
			    DatabaseEntry dataEntry = new DatabaseEntry();
			    Cursor cursor = db.openCursor(null, null);
			    while(cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS){
			    	returnValue.add(new String(dataEntry.getData()));
			    }
			    cursor.close();
				return returnValue;
			}
		  public int getCount(String key){
				DatabaseEntry keyEntry = new DatabaseEntry();
				DatabaseEntry dataEntry = new DatabaseEntry();
			    keyEntry.setData(key.getBytes());
			    Cursor cursor = db.openCursor(null, null);
			    cursor.getSearchKey(keyEntry, dataEntry, LockMode.DEFAULT);
			    int returnValue = 0;
			    try{
			    	returnValue = cursor.count();
			    }catch(IllegalStateException e){
			    	returnValue = 0;
			    }finally{
			    	cursor.close();
				    close();
			    }
				return returnValue;
			}
			
		  
			public static void main(String[] args) throws Exception {
			MyBerkeleyDB mbdb = new MyBerkeleyDB();
			/*mbdb.get("myKey1");
			System.out.println(mbdb.get("myKey1"));*/
			mbdb.setUp("D:\bdb", 1000000);//设置文件夹进行存储,并设置大小
			mbdb.open("myDB");
			//System.out.println(mbdb.get("myKey8"));
			//System.out.println(mbdb.getCount("myKey1"));
			//System.out.println(mbdb.get("myKeyq3"));
			List<String> list1 = mbdb.getAllValue();
			for(String str:list1){
				System.out.println(str);//从库里取出数据
		      //FileUtil.appendString("f:\bdb2.txt", str);
			}
			System.out.println("开始向Berkeley DB中存入数据...");
		    List<String> listData = readFile("d:/。。.txt");//读取数据
			try {
			int i = 0;
			for(String list:listData){
				String key = "myKey" i;
				String value = "myValuesq" list;
				mbdb.put(key , value);//存入数据
				i   ;
			}
 
			} catch (Exception e) {
			e.printStackTrace();
			}
 
			mbdb.close();

}}

0 人点赞