最近学习Berkeley DB Java Edition(JE版)。写一个浅显的例子,入门吧~
至于什么是bdb,请参考
bdb中国研发团队的这篇文章
1: package com.berkeley.dbje;
2:
3: import java.io.File;
4: import java.io.UnsupportedEncodingException;
5:
6: import com.sleepycat.bind.tuple.IntegerBinding;
7: import com.sleepycat.je.Database;
8: import com.sleepycat.je.DatabaseConfig;
9: import com.sleepycat.je.DatabaseEntry;
10: import com.sleepycat.je.Environment;
11: import com.sleepycat.je.EnvironmentConfig;
12: import com.sleepycat.je.LockMode;
13: import com.sleepycat.je.OperationStatus;
14:
15: public class MyBdb {
16:
17: private static MyBdb myBdb = null;
18: private EnvironmentConfig envConfig;
19: private Environment env;
20:
21: private DatabaseConfig dbConfig;
22: private Database db;
23:
24: private MyBdb(){}//prevent others to construct the class object
25:
26: public static MyBdb newInstance(){//single alone pattern,must using static
27: if(myBdb == null){
28: myBdb = new MyBdb();
29: }
30: return myBdb;
31: }
32:
33: //initialize the environment and database
34: public void initEnv(){
35: try{
36: File file = new File("./db/");
37: if(!file.exists()) file.mkdirs();
38:
39: envConfig = new EnvironmentConfig();
40: envConfig.setAllowCreate(true);
41: env = new Environment(file,envConfig);
42:
43: dbConfig = new DatabaseConfig();
44: dbConfig.setAllowCreate(true);
45: db = env.openDatabase(null, "vertext", dbConfig);//use env to open db
46:
47: }catch(Exception e){
48: e.printStackTrace();
49: }
50: }
51:
52: //close env and database
53: public void close(){
54: try{
55: //close the database first! and then close the environemnt
56: if(db!=null){
57: db.close();
58: }
59:
60: if(env!=null){
61: env.close();
62: }
63: }catch(Exception e){
64: e.printStackTrace();
65: }
66: }
67:
68: //insert key(int)&value(String)
69: public int insert(int key,String value){
70: try {
71: DatabaseEntry tkey = new DatabaseEntry();
72: IntegerBinding.intToEntry(key, tkey);//change int to entry(object)
73:
74: //class String can get bytes-->DatabaseEntry(byte[] data
75: DatabaseEntry tvalue = new DatabaseEntry(value.getBytes("UTF-8"));
76:
77: //another method to change String into entry(object)
78: //StringBinding.stringToEntry(value, tvalue);
79:
80: //you have finished changing object to entry,then put to db
81: if(OperationStatus.SUCCESS != db.put(null, tkey, tvalue)){
82: System.out.println("database error:fail to insert data !");
83: return -1;
84: }
85:
86: } catch (UnsupportedEncodingException e) {
87: // TODO Auto-generated catch block
88: e.printStackTrace();
89: return -2;
90: }
91:
92: return 0;
93:
94: }
95:
96: //through key(int) to get data(String)
97: public String getRecord(int key){
98: String value = null;
99: try{
100: DatabaseEntry tkey = new DatabaseEntry();
101: IntegerBinding.intToEntry(key, tkey);
102:
103: DatabaseEntry tvalue = new DatabaseEntry();
104:
105: if(OperationStatus.SUCCESS != db.get(null, tkey, tvalue, LockMode.DEFAULT)){
106: System.out.println("database error:fail to get data records!");
107: }else{//get the record and change
108: value = new String(tvalue.getData());
109: }
110:
111: }catch(Exception e){
112: e.printStackTrace();
113: }
114:
115: return value;
116: }
117:
118: //insert object
119: //object can be considered as key or value!
120: public int insert(int key ,ObjectData oData){
121: try{
122: DatabaseEntry tkey = new DatabaseEntry();
123: IntegerBinding.intToEntry(key, tkey);
124:
125: DatabaseEntry tvalue = new DatabaseEntry();
126: ObjectTupleBinding obind = new ObjectTupleBinding();
127: obind.objectToEntry(oData, tvalue);//encapsulate key&value to One Entry
128:
129: if(OperationStatus.SUCCESS != db.put(null, tkey, tvalue)){
130: System.out.println("database error:fail to insert data");
131: return -1;
132: }
133:
134: }catch(Exception e){
135: e.printStackTrace();
136: return -2;
137: }
138: return 0;
139: }
140:
141: //get the object data
142: public ObjectData getRecord(int key,boolean flag){
143: ObjectData od = null;
144: try{
145: ObjectTupleBinding oBind = new ObjectTupleBinding();
146:
147: DatabaseEntry tkey = new DatabaseEntry();
148: IntegerBinding.intToEntry(key, tkey);
149:
150: DatabaseEntry tvalue = new DatabaseEntry();
151: if(OperationStatus.SUCCESS == db.get(null, tkey, tvalue, LockMode.DEFAULT)){
152: od = (ObjectData)oBind.entryToObject(tvalue);
153: }
154: }catch(Exception e){
155: e.printStackTrace();
156: }
157: return od;
158: }
159:
160:
161: }
以上是berkeley db的基本操作。初始化environment和database等、插入数据、读取数据!
实体对象类ObjectData代码如下:
1: package com.berkeley.dbje;
2:
3: public class ObjectData {
4: private int key;
5: private String name;
6: private double sum;
7: private long length;
8:
9: public ObjectData(int key,String name,double sum,long length){
10: this.key = key;
11: this.name = name;
12: this.sum = sum;
13: this.length = length;
14: }
15:
16: public ObjectData(){
17:
18: }
19:
20: public int getKey() {
21: return key;
22: }
23: public void setKey(int key) {
24: this.key = key;
25: }
26: public String getName() {
27: return name;
28: }
29: public void setName(String name) {
30: this.name = name;
31: }
32: public double getSum() {
33: return sum;
34: }
35: public void setSum(double sum) {
36: this.sum = sum;
37: }
38: public long getLength() {
39: return length;
40: }
41: public void setLength(long length) {
42: this.length = length;
43: }
44:
45: }
要将对象数据存入数据库,首先必须将对象变成DatabaseEntry对象,因此两者只要需要一个转化的类:ObjectDataTupleBinding类:
1: package com.berkeley.dbje;
2:
3: import com.sleepycat.bind.tuple.TupleBinding;
4: import com.sleepycat.bind.tuple.TupleInput;
5: import com.sleepycat.bind.tuple.TupleOutput;
6:
7: /**
8: * identify the regular of data convert
9: * @author sang
10: *
11: */
12: public class ObjectTupleBinding extends TupleBinding{
13:
14: @Override
15: public Object entryToObject(TupleInput in) {
16: //order is very important
17: ObjectData od = new ObjectData();
18: od.setKey(in.readInt());
19: od.setName(in.readString());
20: od.setSum(in.readDouble());
21: od.setLength(in.readLong());
22:
23: return od;
24: }
25:
26: @Override
27: public void objectToEntry(Object obj, TupleOutput out) {
28:
29: //invert object to class ObjectData
30: ObjectData od = (ObjectData)obj;
31:
32: // Write the data to the TupleOutput (a DatabaseEntry).
33: // Order is important. The first data written will be
34: // the first bytes used by the default comparison routines.
35: out.writeInt(od.getKey());
36: out.writeString(od.getName());
37: out.writeDouble(od.getSum());
38: out.writeLong(od.getLength());
39:
40: //then the data will be written to database
41: }
42:
43: }
test代码如下:
1: package com.berkeley.dbje;
2:
3: public class MainTest {
4:
5: /**
6: * @param args
7: */
8: public static void main(String[] args) {
9: // TODO Auto-generated method stub
10: MyBdb myBdb = MyBdb.newInstance();
11: ObjectData od = new ObjectData();
12: myBdb.initEnv();
13: myBdb.insert(12, "ad");
14: myBdb.insert(13,new ObjectData(1,"sa",12,1));
15: System.out.println(myBdb.getRecord(12));
16: od = myBdb.getRecord(13,true);
17: if(od != null){
18: System.out.println(od.getName());
19: }
20: myBdb.close();
21: }
22:
23: }
版权所有,如需转载请留言,并保留本文地址。谢谢~