An example of a simple NoSQL memory database-berkeley DB Basic operation
Recently, due to the development of cloud computing, database technology has evolved from a structured database to a NoSQL database, storing patterns from structured relational storage to key/value storage now in full swing. whichBerkeley DB is a relatively representative memory database product in the above process, and the operation of the database is implemented by the program rather than the SQL statement. Especially in the process of increasing the dynamic data, it is not feasible to try to achieve the expansion by data cutting, because the customer data format is not known beforehand, so it is impossible for service provider to cut data. The non-modal Key/value storage solves the scalability problem of this expansion, because the Key/value storage mode allows the expansion to be automated and does not require prior knowledge of any data format issues. Therefore, the currentNoSQL databases are basically based on key/value storage patterns, such as Amazon's Simpledb,google bigtable. This paper describes how to use it to manipulate key-value pairs of data by describing a more representative memory database Berkeley DB in detail in Key/value mode.
Berkeley DB is an open-source, in-memory database that provides a series of functions that directly access the database, rather than requiring network communication, SQL parsing, and so on, as with relational databases.berkeley db is a high-performance, embedded database programming library,
The keyword/data (key/value) is the basis for Berkeley DB for database management.A few years ago, the word key/value was linked to the hash table. Now, when programmers see the word key/value, it is BigTable, SimpleDB and cloud computing that are immediately linked to the idea. Now, Key/value storage (or key/value Database, cloud storage, etc.) is a very fashionable word, and more and more developers (especially internet companies) are starting to focus on and try to key/value storage. Each key/value pair forms a record (a row in the data table). And the whole database is actually made up of many of these structural units. By using this approach, the developer accesses the database using the APIs provided by Berkeley DB, providing access to the corresponding data with the help of a keyword. Of course, you can also provide key and partial value to query for similar data that matches the criteria. The Berkeley DB low-level implementation mechanism uses B-trees, which can be regarded as hashmap that can store large amounts of data. Berkeley DB through the environmentlike Environmentconfig to manage databases, each Environmentconfig object can manage multiple databases. Here's how Berkeley DB is stored in the Key/value key value pair data. 1, define a serialized Java class, followed by the String and the class mapping <string,java class > stored in Berkeley DB
Package Webspider.berkeleydb;import java.io.serializable;/** * Defines a serialized Java class, placed in Berkeley DB * * @author Typ * * /public class Bean implements Serializable {private static final long Serialversionuid = -6865837107626208563l;private Int Eger id;private String name;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
2. Define the Frontier interface class, define the function interfaces that need to be implemented when storing and acquiring key-value pairs (key/value)
Package webspider.berkeleydb;/** * Defines the function interface to be implemented * * @author typ * */public interface Frontier {/** * Get next record * * @return * @throws Exception */public Bean GetNext () throws exception;/** * Add a record * * @param Bean * @return * @throw S Exception */public boolean putnext (Bean bean) throws Exception;}
3. Define the Abstractfrontier class to encapsulate operations in Berkeley DB
Package Webspider.berkeleydb;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;/** * Package Berkeley DB Operation * * @author Typ * */public abstract class Abstractfrontier {PR Ivate environment env;private Static final String Class_catalog = "Java_class_catalog";p rotected Storedclasscatalog javacatalog;protected db catalogdatabase;protected Database database;/** * Various variables required for initializing the databases * * @param homedirectory * @throws Exception */public abstractfrontier (String homedirectory) throws Exception {Environmentconfig envconfig = new ENV Ironmentconfig ();//Set transaction on, Envconfig.settransactional (TRUE);//Allowcreate property indicates that without a database, You can automatically create database envconfig.setallowcreate (TRUE);//define environment variable, homedirectory refers to the database storage path env = new Environment (New File homedirectory), envconfig);//Set the properties of the database databaseconfig dbconfig = new Databaseconfig ();d bconfig.settrAnsactional (True);d bconfig.setallowcreate (TRUE);//Usually the mappings between Java objects and Java objects are stored separately and placed in different databases// The database that holds the Java object catalogdatabase = Env.opendatabase (null, Class_catalog, dbconfig); javacatalog = new Storedclasscatalog ( Catalogdatabase);//database that holds key/value mappings <string,javabean>database = Env.opendatabase (null, "String_javabean", Dbconfig);} /** * When closing the database, close various links * * @throws Exception */public void Close () throws Exception {database.close (); Javacatalog.close (); E Nv.close ();} /** * Berkeley DB Storage key value pair Operation * * @param key * @param value */protected abstract void put (object key, Object value);/** * Ber Keley db Gets the key value pair operation * * @param key * @return */protected Abstract Object Get (Object key),/** * Berkeley db Delete key value pair Operation * * @par Am Key * @return */protected Abstract Object Delete (object key);}
4, design the implementation of the class, the implementation of database insertion, find and delete functions (in fact, the function of the database is directly can be implemented with Java programs, this is the Berkeley DB Advantage, you can directly to the operation of ordinary Java hash program, operation database)
Package Webspider.berkeleydb;import Java.util.map.entry;import Java.util.set;import Com.sleepycat.bind.entrybinding;import Com.sleepycat.bind.serial.serialbinding;import com.sleepycat.collections.storedmap;/** * Specific Implementation class * * @author Typ * */public class Bdbfrontier extends Abstractfrontier im Plements Frontier {//Establish map, store Key/value key value pair private Storedmap pendingurisdbmap;/** * Initialize Java object Database and object mapping database * @param homedi Rectory * @throws Exception */public bdbfrontier (String homedirectory) throws Exception {super (homedirectory);// Define key and value entryentrybinding keyBinding = new Serialbinding (Javacatalog, String.class); entrybinding valuebinding = new Serialbinding (Javacatalog, bean.class);//map stored in database, database storage key/ Value mapping Pendingurisdbmap = new Storedmap (database, keyBinding, valuebinding,true);} /* * (NON-JAVADOC) * * @see webspider.berkeleydb.frontier#getnext () */public Bean GetNext () throws Exception {Bean Bean = Null;if (!pendingurisdbmap.isempty ()) {set<entry<string, bean>>Entrys = Pendingurisdbmap.entryset (); entry<string, bean> Entry = (entry<string, bean>) Pendingurisdbmap.entryset (). iterator (). Next (); Bean = Entry.getvalue ();d elete (Entry.getkey ());} return bean;} /* (non-javadoc) * @see webspider.berkeleydb.frontier#putnext (Webspider.berkeleydb.Bean) */public Boolean putnext ( Bean Bean) throws Exception {put (Bean.getname (), Bean); return true;} /* * (NON-JAVADOC) * * @see webspider.berkeleydb.abstractfrontier#put (java.lang.Object, * java.lang.Object) */protected void put (object key, Object value) {Pendingurisdbmap.put (key, value);} /* * (NON-JAVADOC) * * @see webspider.berkeleydb.abstractfrontier#get (java.lang.Object) */protected object Get (Object Ke Y) {return pendingurisdbmap.get (key);} /* * (NON-JAVADOC) * * @see webspider.berkeleydb.abstractfrontier#delete (java.lang.Object) */protected Object Delete ( Object key) {return pendingurisdbmap.remove (key);} /** * Test results, you can see the F:\bdb folder generated under the. Jdb and. LCK Berkeley DB related files, data stored in the. JDB * Console output The name value of the data bean just inserted liming */public static void main (string[] args) {try {bdbfrontier frontier = new Bdbfrontier ("F:\\bdb"); Bean bean = new Bean (); Bean.setname ("liming"); Frontier.putnext (bean); System.out.println (Frontier.getnext (). GetName ()); Frontier.close (); catch (Exception e) {e.printstacktrace ();}}}
From the above process, Berkeley db is a good way to implement Key/value, and it is completely
a series of functions that directly access the database, rather than requiring database connectivity, SQL parsing, and so on, like a relational database. Completely with the Java program Operation HashMap is not much different, relatively easy to get started.