目录:
- 数值和字符串类型的对象
- 序列化复杂对象
- 利用Java序列化为ByteArray实现对复杂对象的读写
- 运用SerialBinding来实现对复杂对象的读写
- 运用SerialBinding来实现对复杂对象的读写
[一]、数值和字符串类型的对象
演示代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
package com.micmiu.berkeley.demo; import java.io.File; import com.sleepycat.bind.EntryBinding; import com.sleepycat.bind.tuple.TupleBinding; 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; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class BindAPI2Demo { public static void testBind(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; EnvironmentConfig envCfg = new EnvironmentConfig(); envCfg.setAllowCreate(true); envCfg.setTransactional(true); try { mydbEnv = new Environment(new File(envHomePath), envCfg); DatabaseConfig dbCfg = new DatabaseConfig(); dbCfg.setAllowCreate(true); dbCfg.setTransactional(true); System.out.println("openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); MyBindTest<String> stringTest = new MyBindTest<String>(myDatabase, "myString", String.class, "www.micmiu.com"); stringTest.runDemo(); MyBindTest<Boolean> booleanTest = new MyBindTest<Boolean>( myDatabase, "myBoolean", Boolean.class, Boolean.TRUE); booleanTest.runDemo(); MyBindTest<Byte> byteTest = new MyBindTest<Byte>(myDatabase, "myByte", Byte.class, Byte.valueOf("65")); byteTest.runDemo(); MyBindTest<Short> shortTest = new MyBindTest<Short>(myDatabase, "myShort", Short.class, Short.valueOf((short) 1990)); shortTest.runDemo(); MyBindTest<Integer> integerTest = new MyBindTest<Integer>( myDatabase, "myInteger", Integer.class, Integer.valueOf(1984)); integerTest.runDemo(); MyBindTest<Long> longTest = new MyBindTest<Long>(myDatabase, "myLong", Long.class, new Long(123456789L)); longTest.runDemo(); MyBindTest<Float> floatTest = new MyBindTest<Float>(myDatabase, "myFloat", Float.class, Float.valueOf(172.5f)); floatTest.runDemo(); MyBindTest<Double> doubleTest = new MyBindTest<Double>(myDatabase, "myDouble", Double.class, Double.valueOf(60.5d)); doubleTest.runDemo(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != myDatabase) { myDatabase.close(); } if (null != mydbEnv) { // 在关闭环境前清理下日志 mydbEnv.cleanLog(); mydbEnv.close(); mydbEnv = null; } } } static class MyBindTest<T> { private Database myDatabase; private String keyName; private T value; private Class<T> clazz; public MyBindTest(Database myDatabase, String keyName, Class<T> clazz, T value) { this.myDatabase = myDatabase; this.keyName = keyName; this.clazz = clazz; this.value = value; } public void runDemo() throws Exception { System.out.println(" ------------------------------------ "); String type = clazz.getSimpleName(); System.out.println(" ---- > Bind Object Type : " + type); DatabaseEntry keyEntry = new DatabaseEntry( keyName.getBytes("utf-8")); System.out.println("put key=" + keyName + "," + type + " value = " + value); DatabaseEntry valEntry = new DatabaseEntry(); EntryBinding<T> myBinding = TupleBinding.getPrimitiveBinding(clazz); myBinding.objectToEntry(value, valEntry); OperationStatus status = myDatabase.put(null, keyEntry, valEntry); System.out.println("put status" + status); System.out.println("get data by key=" + keyName); DatabaseEntry findEntry = new DatabaseEntry(); status = myDatabase .get(null, keyEntry, findEntry, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { T value = myBinding.entryToObject(findEntry); System.out.println("get key = " + keyName + " , value = " + value); } else { System.out.println("No record found for key = " + keyName); } } } /** * @param args */ public static void main(String[] args) { System.out.println(" ==== > Demo Test Start < ===="); String envHomePath = "D:/test/berkeleydata"; String databaseName = "micmiu-demo"; BindAPI2Demo.testBind(envHomePath, databaseName); System.out.println(" ==== > Demo Test End < ===="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
==== > Demo Test Start < ==== openDatabase: micmiu-demo ------------------------------------ ---- > Bind Object Type : String put key=myString,String value = www.micmiu.com put statusOperationStatus.SUCCESS get data by key=myString get key = myString , value = www.micmiu.com ------------------------------------ ---- > Bind Object Type : Boolean put key=myBoolean,Boolean value = true put statusOperationStatus.SUCCESS get data by key=myBoolean get key = myBoolean , value = true ------------------------------------ ---- > Bind Object Type : Byte put key=myByte,Byte value = 65 put statusOperationStatus.SUCCESS get data by key=myByte get key = myByte , value = 65 ------------------------------------ ---- > Bind Object Type : Short put key=myShort,Short value = 1990 put statusOperationStatus.SUCCESS get data by key=myShort get key = myShort , value = 1990 ------------------------------------ ---- > Bind Object Type : Integer put key=myInteger,Integer value = 1984 put statusOperationStatus.SUCCESS get data by key=myInteger get key = myInteger , value = 1984 ------------------------------------ ---- > Bind Object Type : Long put key=myLong,Long value = 123456789 put statusOperationStatus.SUCCESS get data by key=myLong get key = myLong , value = 123456789 ------------------------------------ ---- > Bind Object Type : Float put key=myFloat,Float value = 172.5 put statusOperationStatus.SUCCESS get data by key=myFloat get key = myFloat , value = 172.5 ------------------------------------ ---- > Bind Object Type : Double put key=myDouble,Double value = 60.5 put statusOperationStatus.SUCCESS get data by key=myDouble get key = myDouble , value = 60.5 ==== > Demo Test End < ==== |
[二]、序列化复杂对象
1. 利用Java序列化为ByteArray实现对复杂对象的读写
演示代码中自定义的java bean: MyData.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.micmiu.berkeley.demo.vo; import java.io.Serializable; import java.util.Date; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class MyData implements Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 2476140391638320314L; private String description; private int dataint; private Double dataDouble; private Date dataDate; public MyData() { } public MyData(int dataint, Double dataDouble, Date dataDate) { this.dataint = dataint; this.dataDouble = dataDouble; this.dataDate = dataDate; } public String getDescription() { return description; } public int getDataint() { return dataint; } public Double getDataDouble() { return dataDouble; } public Date getDataDate() { return dataDate; } public void setDescription(String description) { this.description = description; } public void setDataint(int dataint) { this.dataint = dataint; } public void setDataDouble(Double dataDouble) { this.dataDouble = dataDouble; } public void setDataDate(Date dataDate) { this.dataDate = dataDate; } @Override public String toString() { return "MyData [description=" + description + ", dataint=" + dataint + ", dataDouble=" + dataDouble + ", dataDate=" + dataDate + "]"; } } |
读写数据的演示代码:ObjectByteArrayDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
package com.micmiu.berkeley.demo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; import com.micmiu.berkeley.demo.vo.MyData; 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; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class ObjectByteArrayDemo { /** * @param envHomePath */ public static void testSerialBind(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; EnvironmentConfig envCfg = new EnvironmentConfig(); envCfg.setAllowCreate(true); envCfg.setTransactional(true); try { mydbEnv = new Environment(new File(envHomePath), envCfg); DatabaseConfig dbCfg = new DatabaseConfig(); dbCfg.setAllowCreate(true); dbCfg.setTransactional(true); System.out.println("openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ---- > demo ByteArray "); String beanKey = "first-ByteArray-demo"; DatabaseEntry beanKeyEntry = new DatabaseEntry( beanKey.getBytes("utf-8")); DatabaseEntry beanValEntry = new DatabaseEntry(); MyData data = new MyData(1234, 101.7d, new Date()); data.setDescription("Hello,ByteArray welcome www.micmiu.com."); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(data); } catch (IOException e) { e.printStackTrace(); } beanValEntry.setData(baos.toByteArray()); System.out.println(" ---- > put key and value"); System.out.println("put bean key : " + beanKey); OperationStatus status = myDatabase.put(null, beanKeyEntry, beanValEntry); System.out.println("put operation status:" + status); if (status == OperationStatus.SUCCESS) { System.out.println("put javabean success"); } else { System.out.println("put javabean failed"); } System.out.println(" ---- > get value of the key"); DatabaseEntry valEntry4Get = new DatabaseEntry(); status = myDatabase.get(null, beanKeyEntry, valEntry4Get, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { MyData getData = null; ByteArrayInputStream bais = new ByteArrayInputStream( valEntry4Get.getData()); try { ObjectInputStream ois = new ObjectInputStream(bais); getData = (MyData) ois.readObject(); } catch (Exception e) { e.printStackTrace(); } System.out.println("get success key value:\n" + getData); } else { System.out.println("get key value failed."); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != myDatabase) { myDatabase.close(); } if (null != mydbEnv) { // 在关闭环境前清理下日志 mydbEnv.cleanLog(); mydbEnv.close(); mydbEnv = null; } } } /** * @param args */ public static void main(String[] args) { System.out.println(" ==== > Demo Test Start < ===="); String envHomePath = "D:/test/berkeleydata"; String databaseName = "micmiu-demo"; ObjectByteArrayDemo.testSerialBind(envHomePath, databaseName); System.out.println(" ==== > Demo Test End < ===="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 |
==== > Demo Test Start < ==== openDatabase: micmiu-demo ---- > demo ByteArray ---- > put key and value put bean key : first-ByteArray-demo put operation status:OperationStatus.SUCCESS put javabean success ---- > get value of the key get success key value: MyData [description=Hello,ByteArray welcome www.micmiu.com., dataint=1234, dataDouble=101.7, dataDate=Mon Aug 27 12:39:44 CST 2012] ==== > Demo Test End < ==== |
2.运用SerialBinding来实现对复杂对象的读写
演示代码:ObjectSerialBindingDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
package com.micmiu.berkeley.demo; import java.io.File; import java.util.Date; import com.micmiu.berkeley.demo.vo.MyData; import com.sleepycat.bind.EntryBinding; import com.sleepycat.bind.serial.ClassCatalog; import com.sleepycat.bind.serial.SerialBinding; import com.sleepycat.bind.serial.StoredClassCatalog; 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; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class ObjectSerialBindingDemo { /** * @param envHomePath */ public static void testSerialBind(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; EnvironmentConfig envCfg = new EnvironmentConfig(); envCfg.setAllowCreate(true); envCfg.setTransactional(true); try { mydbEnv = new Environment(new File(envHomePath), envCfg); DatabaseConfig dbCfg = new DatabaseConfig(); dbCfg.setAllowCreate(true); dbCfg.setTransactional(true); System.out.println("openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ---- > demo SerialBinding"); ClassCatalog catalog = new StoredClassCatalog(myDatabase); EntryBinding<MyData> valueBinding = new SerialBinding<MyData>( catalog, MyData.class); String beanKey = "first-serialBind-demo"; DatabaseEntry beanKeyEntry = new DatabaseEntry( beanKey.getBytes("utf-8")); DatabaseEntry beanValEntry = new DatabaseEntry(); MyData data = new MyData(1234, 101.7d, new Date()); data.setDescription("Hello,SerialBinding welcome www.micmiu.com."); valueBinding.objectToEntry(data, beanValEntry); System.out.println(" ---- > put key and value"); System.out.println("put bean key : " + beanKey); OperationStatus status = myDatabase.put(null, beanKeyEntry, beanValEntry); System.out.println("put operation status:" + status); if (status == OperationStatus.SUCCESS) { System.out.println("put javabean success"); } else { System.out.println("put javabean failed"); } System.out.println(" ---- > get value of the key"); DatabaseEntry valEntry4Get = new DatabaseEntry(); status = myDatabase.get(null, beanKeyEntry, valEntry4Get, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { MyData getData = valueBinding.entryToObject(valEntry4Get); System.out.println("get success key value:\n" + getData); } else { System.out.println("get key value failed."); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != myDatabase) { myDatabase.close(); } if (null != mydbEnv) { // 在关闭环境前清理下日志 mydbEnv.cleanLog(); mydbEnv.close(); mydbEnv = null; } } } /** * @param args */ public static void main(String[] args) { System.out.println(" ==== > Demo Test Start < ===="); String envHomePath = "D:/test/berkeleydata"; String databaseName = "micmiu-demo"; ObjectSerialBindingDemo.testSerialBind(envHomePath, databaseName); System.out.println(" ==== > Demo Test End < ===="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 |
==== > Demo Test Start < ==== openDatabase: micmiu-demo ---- > demo SerialBinding ---- > put key and value put bean key : first-serialBind-demo put operation status:OperationStatus.SUCCESS put javabean success ---- > get value of the key get success key value: MyData [description=Hello,SerialBinding welcome www.micmiu.com., dataint=1234, dataDouble=101.7, dataDate=Mon Aug 27 13:58:28 CST 2012] ==== > Demo Test End < ==== |
3.自定义TupleBinding来对复杂对象的读写
演示代码:ObjectCustomTupleBindingDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
package com.micmiu.berkeley.demo; import java.io.File; import java.util.Date; import com.micmiu.berkeley.demo.vo.MyData; import com.sleepycat.bind.tuple.TupleBinding; 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; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class ObjectCustomTupleBindingDemo { /** * @param envHomePath */ public static void testSerialBind(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; EnvironmentConfig envCfg = new EnvironmentConfig(); envCfg.setAllowCreate(true); envCfg.setTransactional(true); try { mydbEnv = new Environment(new File(envHomePath), envCfg); DatabaseConfig dbCfg = new DatabaseConfig(); dbCfg.setAllowCreate(true); dbCfg.setTransactional(true); System.out.println("openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ---- > demo Custom Binding"); TupleBinding<MyData> valueBinding = new MyTupleBinding(); String beanKey = "first-customBind-demo"; DatabaseEntry beanKeyEntry = new DatabaseEntry( beanKey.getBytes("utf-8")); DatabaseEntry beanValEntry = new DatabaseEntry(); MyData data = new MyData(1234, null, new Date()); data.setDescription("Hello,CustomBinding welcome www.micmiu.com."); valueBinding.objectToEntry(data, beanValEntry); System.out.println(" ---- > put key and value"); System.out.println("put bean key : " + beanKey); OperationStatus status = myDatabase.put(null, beanKeyEntry, beanValEntry); System.out.println("put operation status:" + status); if (status == OperationStatus.SUCCESS) { System.out.println("put javabean success"); } else { System.out.println("put javabean failed"); } System.out.println(" ---- > get value of the key"); DatabaseEntry valEntry4Get = new DatabaseEntry(); status = myDatabase.get(null, beanKeyEntry, valEntry4Get, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { MyData getData = valueBinding.entryToObject(valEntry4Get); System.out.println("get success key value:\n" + getData); } else { System.out.println("get key value failed."); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != myDatabase) { myDatabase.close(); } if (null != mydbEnv) { // 在关闭环境前清理下日志 mydbEnv.cleanLog(); mydbEnv.close(); mydbEnv = null; } } } /** * @param args */ public static void main(String[] args) { System.out.println(" ==== > Demo Test Start < ===="); String envHomePath = "D:/test/berkeleydata"; String databaseName = "micmiu-demo"; ObjectCustomTupleBindingDemo.testSerialBind(envHomePath, databaseName); System.out.println(" ==== > Demo Test End < ===="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 |
==== > Demo Test Start < ==== openDatabase: micmiu-demo ---- > demo Custom Binding ---- > put key and value put bean key : first-customBind-demo put operation status:OperationStatus.SUCCESS put javabean success ---- > get value of the key get success key value: MyData [description=Hello,CustomBinding welcome www.micmiu.com., dataint=1234, dataDouble=null, dataDate=Mon Aug 27 13:59:24 CST 2012] ==== > Demo Test End < ==== |
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/nosql/berkeley/berkeley-bind-apis/
请教一下:
SerialBinding keyBinding=new SerialBinding(catalog,String.class);
SerialBinding valueBinding=new SerialBinding(catalog,String.class);
StoredMap map=new StoredMap(database,keyBinding,valueBinding,true);
上面代码让map对database进行了映射,怎样使map对数据库database进行更新呢?