目录:
- Environment
- Databases
- Catalog
- String数据的CRUD操作
- 统计信息的获取
代码:SimpleDemo.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 |
package com.micmiu.berkeley.demo; import java.io.File; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class SimpleDemo { /** * @param envHomePath */ public static void testEnvironment(String envHomePath) { Environment mydbEnv = null; System.out.println(" ----> EnvironmentConfig init"); // 创建一个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 init"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ----> Env Config: " + mydbEnv.getConfig()); System.out.println(" ----> Env Home: " + mydbEnv.getHome().getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } finally { 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"; SimpleDemo.testEnvironment(envHomePath); System.out.println(" ===== > Demo Test End < ====="); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 |
===== > Demo Test Start < ===== ----> EnvironmentConfig init ----> Environment init ----> Env Config: allowCreate=true cacheSize=20971520 txnNoSync=false txnWriteNoSync=false {je.env.isTransactional=true, je.maxMemory=20971520} ----> Env Home: D:\test\berkeleydata ===== > Demo Test End < ===== |
Env Home目录下数据日志文件:
1 2 3 4 5 6 7 |
D:\test\berkeleydata>tree /F 卷 work 的文件夹 PATH 列表 卷序列号为 2AF7-9BD9 D:. 00000000.jdb je.info.0 je.lck |
演示代码:SimpleDemo.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 |
package com.micmiu.berkeley.demo; import java.io.File; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class SimpleDemo { /** * @param envHomePath */ public static void testDatabase(String envHomePath) { Environment mydbEnv = null; String dbName1 = "micmiu-michael"; String dbName2 = "micmiu-sun"; Database myDatabase1 = null; Database myDatabase2 = null; System.out.println(" ----> EnvironmentConfig init"); // 创建一个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 init"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ----> DatabaseConfig init"); DatabaseConfig dbCfg = new DatabaseConfig(); // 如果数据库不存在则创建一个 dbCfg.setAllowCreate(true); // 如果设置为true,则支持事务处理,默认是false,不支持事务 dbCfg.setTransactional(true); System.out.println(" ---->openDatabase:名称为 micmiu-michael"); myDatabase1 = mydbEnv.openDatabase(null, dbName1, dbCfg); System.out.println(" ----> name: " + myDatabase1.getDatabaseName()); System.out.println(" ----> config: " + myDatabase1.getConfig()); System.out.println(" ---->openDatabase:名称为 micmiu-sun"); myDatabase2 = mydbEnv.openDatabase(null, dbName2, dbCfg); System.out.println(" ----> name: " + myDatabase2.getDatabaseName()); System.out.println(" ----> config: " + myDatabase2.getConfig()); } catch (Exception e) { e.printStackTrace(); } finally { if (null != myDatabase1) { myDatabase1.close(); } if (null != myDatabase2) { myDatabase2.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"; SimpleDemo.testDatabase(envHomePath); 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 |
===== > Demo Test Start < ===== ----> EnvironmentConfig init ----> Environment init ----> DatabaseConfig init ---->openDatabase:名称为 micmiu-michael ----> name: micmiu-michael ----> config: allowCreate=true exclusiveCreate=false transactional=true readOnly=false duplicatesAllowed=false deferredWrite=false temporary=false keyPrefixingEnabled=false ---->openDatabase:名称为 micmiu-sun ----> name: micmiu-sun ----> config: allowCreate=true exclusiveCreate=false transactional=true readOnly=false duplicatesAllowed=false deferredWrite=false temporary=false keyPrefixingEnabled=false ===== > Demo Test End < ===== |
演示代码:SimpleDemo.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 |
package com.micmiu.berkeley.demo; import java.io.File; import com.sleepycat.bind.serial.StoredClassCatalog; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class SimpleDemo { /** * @param envHomePath */ public static void testCatalog(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; StoredClassCatalog catalog = null; System.out.println(" ----> EnvironmentConfig init"); // 创建一个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 init"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ----> DatabaseConfig init"); DatabaseConfig dbCfg = new DatabaseConfig(); // 如果数据库不存在则创建一个 dbCfg.setAllowCreate(true); // 如果设置为true,则支持事务处理,默认是false,不支持事务 dbCfg.setTransactional(true); System.out.println(" ----> open Database: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ----> StoredClassCatalog init"); catalog = new StoredClassCatalog(myDatabase); System.out.println(" ----> catalog: " + catalog.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (null != myDatabase) { myDatabase.close(); } if (null != catalog) { catalog.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 catalogName = "micmiu-catalog"; SimpleDemo.testCatalog(envHomePath, catalogName); System.out.println(" ===== > Demo Test End < ====="); } } |
运行结果:
1 2 3 4 5 6 7 8 |
===== > Demo Test Start < ===== ----> EnvironmentConfig init ----> Environment init ----> DatabaseConfig init ----> open Database: micmiu-catalog ----> StoredClassCatalog init ----> catalog: com.sleepycat.bind.serial.StoredClassCatalog@1e1a408 ===== > Demo Test End < ===== |
演示代码:SimpleDemo.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 125 126 127 |
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 testSimpleData(String envHomePath, String databaseName) { Environment mydbEnv = null; Database myDatabase = null; System.out.println(" ---- > EnvironmentConfig init"); // 创建一个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 init"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ---- > DatabaseConfig init"); DatabaseConfig dbCfg = new DatabaseConfig(); // 如果数据库不存在则创建一个 dbCfg.setAllowCreate(true); // 如果设置为true,则支持事务处理,默认是false,不支持事务 dbCfg.setTransactional(true); System.out.println(" ---- > open Database: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ---- > test CRUD "); String key = "myfirstdata"; String value = "Hello Berkeley welcome to www.micmiu.com"; DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("utf-8")); DatabaseEntry valEntry = new DatabaseEntry(value.getBytes("utf-8")); System.out.println(" ---- > put data with key :" + key); OperationStatus status = myDatabase.put(null, keyEntry, valEntry); System.out.println(" ---- > put status: " + status); System.out.println(" ---- > get key :" + key); DatabaseEntry valEntry4Get = new DatabaseEntry(); status = myDatabase.get(null, keyEntry, valEntry4Get, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { value = new String(valEntry4Get.getData(), "utf-8"); System.out.println(" ---- > get key success value:" + value); } else { System.out.println(" ---- > get key failed."); } System.out.println(" ---- > update data with key :" + key); value = "Hello Michael welcome to www.micmiu.com"; status = myDatabase.put(null, keyEntry, new DatabaseEntry(value.getBytes("utf-8"))); System.out.println(" ---- > update status: " + status); System.out.println(" ---- > after update get key :" + key); status = myDatabase.get(null, keyEntry, valEntry4Get, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { value = new String(valEntry4Get.getData(), "utf-8"); System.out.println(" ---- > get success key value:" + value); } else { System.out.println(" ---- > get key value failed."); } System.out.println(" ---- > delete key :" + key); status = myDatabase.delete(null, keyEntry); System.out.println(" ---- > delete status: " + status); System.out.println(" ---- > after delete get key :" + key); status = myDatabase.get(null, keyEntry, valEntry4Get, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { value = new String(valEntry4Get.getData(), "utf-8"); System.out.println(" ---- > get success key value:" + value); } 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-michael"; SimpleDemo.testSimpleData(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 |
==== > Demo Test Start < ==== ---- > EnvironmentConfig init ---- > Environment init ---- > DatabaseConfig init ---- > open Database: micmiu-michael ---- > test CRUD ---- > put data with key :myfirstdata ---- > put status: OperationStatus.SUCCESS ---- > get key :myfirstdata ---- > get key success value:Hello Berkeley welcome to www.micmiu.com ---- > update data with key :myfirstdata ---- > update status: OperationStatus.SUCCESS ---- > after update get key :myfirstdata ---- > get success key value:Hello Michael welcome to www.micmiu.com ---- > delete key :myfirstdata ---- > delete status: OperationStatus.SUCCESS ---- > after delete get key :myfirstdata ---- > get key value failed. ==== > Demo Test End < ==== |
演示代码:SimpleDemo.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 |
package com.micmiu.berkeley.demo; import java.io.File; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class SimpleDemo { /** * @param envHomePath */ public static void testStats(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); envCfg.setConfigParam(EnvironmentConfig.NODE_MAX_ENTRIES, "4"); try { System.out.println(" ---- > Environment create"); mydbEnv = new Environment(new File(envHomePath), envCfg); System.out.println(" ---- > Environment stats: "); System.out.println(mydbEnv.getStats(null).toString()); System.out.println(" ---- > DatabaseConfig init"); DatabaseConfig dbCfg = new DatabaseConfig(); // 如果数据库不存在则创建一个 dbCfg.setAllowCreate(true); // 如果设置为true,则支持事务处理,默认是false,不支持事务 dbCfg.setTransactional(true); System.out.println(" ---- > openDatabase: " + databaseName); myDatabase = mydbEnv.openDatabase(null, databaseName, dbCfg); System.out.println(" ---- > database stats: "); System.out.println(myDatabase.getStats(null)); } 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-michael"; SimpleDemo.testStats(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 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 |
==== > Demo Test Start < ==== ---- > EnvironmentConfig create ---- > Environment create ---- > Environment stats: I/O: Log file opens, fsyncs, reads, writes, cache misses. bufferBytes=1,310,718 endOfLog=0x0/0x1d7b nBytesReadFromWriteQueue=0 nBytesWrittenFromWriteQueue=0 nCacheMiss=14 nFSyncRequests=0 nFSyncTimeouts=0 nFSyncs=0 nFileOpens=2 nLogBuffers=3 nNotResident=14 nOpenFiles=0 nRandomReadBytes=2,048 nRandomReads=1 nRandomWriteBytes=0 nRandomWrites=0 nReadsFromWriteQueue=0 nRepeatFaultReads=0 nSequentialReadBytes=120,832 nSequentialReads=26 nSequentialWriteBytes=0 nSequentialWrites=0 nTempBufferWrites=0 nWriteQueueOverflow=0 nWriteQueueOverflowFailures=0 nWritesFromWriteQueue=0 Cache: Current size, allocations, and eviction activity. adminBytes=101 cacheTotalBytes=1,321,345 dataBytes=10,524 lockBytes=0 nBINsStripped=0 nEvictPasses=0 nNodesEvicted=0 nNodesScanned=0 nNodesSelected=0 nRootNodesEvicted=0 nSharedCacheEnvironments=0 requiredEvictBytes=0 sharedCacheTotalBytes=0 Cleaning: Frequency and extent of log file cleaning activity. cleanerBackLog=0 fileDeletionBacklog=0 nCleanerDeletions=0 nCleanerEntriesRead=0 nCleanerRuns=0 nClusterLNsProcessed=0 nINsCleaned=0 nINsDead=0 nINsMigrated=0 nINsObsolete=0 nLNQueueHits=0 nLNsCleaned=0 nLNsDead=0 nLNsLocked=0 nLNsMarked=0 nLNsObsolete=0 nMarkLNsProcessed=0 nPendingLNsLocked=0 nPendingLNsProcessed=0 nRepeatIteratorReads=0 nToBeCleanedLNsProcessed=0 totalLogSize=7,554 Node Compression: Removal and compression of internal btree nodes. cursorsBins=0 dbClosedBins=0 inCompQueueSize=0 nonEmptyBins=0 processedBins=0 splitBins=0 Checkpoints: Frequency and extent of checkpointing activity. lastCheckpointEnd=0x0/0x1d7b lastCheckpointId=8 lastCheckpointStart=0x0/0x0 nCheckpoints=0 nDeltaINFlush=0 nFullBINFlush=0 nFullINFlush=0 Environment: General environment wide statistics. btreeRelatchesRequired=1 Locks: Locks held by data operations, latching contention on lock table. nLatchAcquireNoWaitUnsuccessful=0 nLatchAcquiresNoWaitSuccessful=0 nLatchAcquiresNoWaiters=0 nLatchAcquiresSelfOwned=0 nLatchAcquiresWithContention=0 nLatchReleases=0 nOwners=0 nReadLocks=0 nRequests=16 nTotalLocks=0 nWaiters=0 nWaits=0 nWriteLocks=0 ---- > DatabaseConfig init ---- > openDatabase: micmiu-michael ---- > database stats: BTree: Composition of btree, types and counts of nodes. binCount=1 binsByLevel=[ level 1: count = 1 ] dbinCount=0 dbinsByLevel=[] deletedLNCount=0 dinCount=0 dinsByLevel=[] dupCountLNCount=0 duplicateTreeMaxDepth=0 inCount=1 insByLevel=[ level 2: count = 1 ] lnCount=4 mainTreeMaxDepth=2 BTree: Composition of btree, types and counts of nodes. nRootSplits=0 relatchesRequired=0 ==== > Demo Test End < ==== |
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
博主您好!看了您的博文我有两个疑问想请教一下,你的envHomePath 和databaseName这两个变量是什么意思?我的理解是envHomePath是一个存放数据的文件夹,databaseName是berkeleyDB数据库。请指教,很急,谢谢!