目录:
- 写入数据的几种方法
- 读取数据的几种方法
[一]、写入数据的几种方法
- Database.put(): 向数据库写入数据,如果不支持重复记录,则会覆盖更新key对应的已有记录
- Database.putNoOverwrite():向数据库写入数据,但是如果key已经存在,不会覆盖已有数据(即使数据库支持重复key)
- Database.putNoDupData():向数据库写入数据(该方法仅用于支持重复key的数据库),如果key和value对应的记录已经存在,那么操作结果是:OperationStatus.KEYEXIST
演示代码:
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 |
package com.micmiu.berkeley.demo; import java.io.File; 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.OperationStatus; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class SimpleDemo { /** * @param envHomePath */ public static void testPutData(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; System.out.println(" ---- > EnvironmentConfig create"); // 创建一个EnvironmentConfig配置对象 EnvironmentConfig envCfg = new EnvironmentConfig(); // 如果设置了true则表示当数据库环境不存在时候重新创建一个数据库环境,默认为false. envCfg.setAllowCreate(true); // 设置数据库缓存大小 envCfg.setCacheSize(1024 * 1024 * 20); // 事务支持,如果为true,则表示当前环境支持事务处理,默认为false,不支持事务处理。 envCfg.setTransactional(true); try { System.out.println(" ---- > Environment create"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ---- > DatabaseConfig create"); DatabaseConfig dbCfg = new DatabaseConfig(); // 如果数据库不存在则创建一个 dbCfg.setAllowCreate(true); // 如果设置为true,则支持事务处理,默认是false,不支持事务 dbCfg.setTransactional(true); dbCfg.setSortedDuplicates(true); System.out.println(" ---- > openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ---- > put a key with a value"); String myKey = "myTestKey"; String myVal = "http://www.micmiu.com"; DatabaseEntry keyEntry = new DatabaseEntry(myKey.getBytes("utf-8")); DatabaseEntry valEntry = new DatabaseEntry(myVal.getBytes("utf-8")); OperationStatus status = myDatabase.put(null, keyEntry, valEntry); System.out.println(" ---- > put status:" + status); //putNoOverwrite如果插入相同key的数据, 即使是支持重复key的数据库,操作结果也是KEYEXIST System.out.println(" ---- > putNoOverwrite with the exit key"); status = myDatabase.putNoOverwrite(null, keyEntry, valEntry); System.out.println(" ---- > putNoOverwrite status:" + status); //putNoDupData只能用于 DatabaseConfig.setSortedDuplicates(true)的数据库 System.out.println(" ---- > putNoDupData the same key and value"); status = myDatabase.putNoDupData(null, keyEntry, valEntry); System.out.println(" ---- > putNoDupData status:" + status); System.out .println(" ---- > putNoDupData the same key with new value"); DatabaseEntry val2Entry = new DatabaseEntry( "www.micmiu.com".getBytes("utf-8")); status = myDatabase.putNoDupData(null, keyEntry, val2Entry); System.out.println(" ---- > putNoDupData status:" + status); } 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"; SimpleDemo.testPutData(envHomePath, databaseName); System.out.println(" ==== > Demo Test End < ===="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
==== > Demo Test Start < ==== ---- > EnvironmentConfig create ---- > Environment create ---- > DatabaseConfig create ---- > openDatabase: micmiu-demo ---- > put a key with a value ---- > put status:OperationStatus.SUCCESS ---- > putNoOverwrite with the exit key ---- > putNoOverwrite status:OperationStatus.KEYEXIST ---- > putNoDupData the same key and value ---- > putNoDupData status:OperationStatus.KEYEXIST ---- > putNoDupData the same key with new value ---- > putNoDupData status:OperationStatus.SUCCESS ==== > Demo Test End < ==== |
[二]、读取数据的几种方法
- Database.get() :检索key对应的记录,如果没有找到,操作结果返回:OperationStatus.NOTFOUND
- Database.getSearchBoth() :根据key和value 检索数据库记录,如果没有找到,操作结果返回:OperationStatus.NOTFOUND
演示代码:
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 |
package com.micmiu.berkeley.demo; import java.io.File; 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 SimpleDemo { /** * @param envHomePath */ public static void testGetData(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; System.out.println(" ---- > EnvironmentConfig create"); // 创建一个EnvironmentConfig配置对象 EnvironmentConfig envCfg = new EnvironmentConfig(); // 如果设置了true则表示当数据库环境不存在时候重新创建一个数据库环境,默认为false. envCfg.setAllowCreate(true); // 设置数据库缓存大小 envCfg.setCacheSize(1024 * 1024 * 20); // 事务支持,如果为true,则表示当前环境支持事务处理,默认为false,不支持事务处理。 envCfg.setTransactional(true); try { System.out.println(" ---- > Environment create"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ---- > DatabaseConfig create"); DatabaseConfig dbCfg = new DatabaseConfig(); // 如果数据库不存在则创建一个 dbCfg.setAllowCreate(true); // 如果设置为true,则支持事务处理,默认是false,不支持事务 dbCfg.setTransactional(true); dbCfg.setSortedDuplicates(true); System.out.println(" ---- > openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); String myKey = "myTestKey"; System.out.println(" ---- > get data by key = " + myKey); DatabaseEntry keyEntry = new DatabaseEntry(myKey.getBytes("utf-8")); DatabaseEntry valEntry = new DatabaseEntry(); OperationStatus status = myDatabase.get(null, keyEntry, valEntry, LockMode.DEFAULT); System.out.println(" ---- > get status:" + status); if (status == OperationStatus.SUCCESS) { String foundData = new String(valEntry.getData(), "UTF-8"); System.out.println(" ---- > get key = " + myKey + " value = " + foundData); } else { System.out .println(" ---- > No record found for key = " + myKey); } String myVal = "www.micmiu.com"; System.out.println(" ---- > getSearchBoth by key = " + myKey + " and value = " + myVal); DatabaseEntry dataEntry = new DatabaseEntry(myVal.getBytes("utf-8")); status = myDatabase.getSearchBoth(null, keyEntry, dataEntry, LockMode.DEFAULT); System.out.println(" ---- > getSearchBoth status:" + status); myVal = "micmiu.com"; System.out.println(" ---- > getSearchBoth by key = " + myKey + " and value = " + myVal); dataEntry = new DatabaseEntry(myVal.getBytes("utf-8")); status = myDatabase.getSearchBoth(null, keyEntry, dataEntry, LockMode.DEFAULT); System.out.println(" ---- > getSearchBoth status:" + status); } 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"; SimpleDemo.testGetData(envHomePath, databaseName); System.out.println(" ==== > Demo Test End < ===="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
==== > Demo Test Start < ==== ---- > EnvironmentConfig create ---- > Environment create ---- > DatabaseConfig create ---- > openDatabase: micmiu-demo ---- > get data by key = myTestKey ---- > get status:OperationStatus.SUCCESS ---- > get key = myTestKey value = http://www.micmiu.com ---- > getSearchBoth by key = myTestKey and value = www.micmiu.com ---- > getSearchBoth status:OperationStatus.SUCCESS ---- > getSearchBoth by key = myTestKey and value = micmiu.com ---- > getSearchBoth status:OperationStatus.NOTFOUND ==== > Demo Test End < ==== |
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/nosql/berkeley/berkeley-write-read-data/
1 条评论。