除了关系型数据库外,还有key-value型数据库可以选择,在大型的查询系统中,key-value型数据库可以出色的完成某些任务。
Oracle的Berkeley DB就是一种key-value型数据库,本文简要介绍在java中使用Berkeley DB。
PartKey.java:
import java.io.Serializable; public class PartKey implements Serializable { private String number; public PartKey(String number) { this.number = number; } public final String getNumber() { return number; } public String toString() { return "[PartKey: number=" + number + ']'; } }
Weight.java
public class Weight implements Serializable { public final static String GRAMS = "grams"; public final static String OUNCES = "ounces"; private double amount; private String units; public Weight(double amount, String units) { this.amount = amount; this.units = units; } public final double getAmount() { return amount; } public final String getUnits() { return units; } public String toString() { return "[" + amount + ' ' + units + ']'; } }
PartData.java
public class PartData implements Serializable { private String name; private String color; private Weight weight; private String city; public PartData(String name, String color, Weight weight, String city) { this.name = name; this.color = color; this.weight = weight; this.city = city; } public final String getName() { return name; } public final String getColor() { return color; } public final Weight getWeight() { return weight; } public final String getCity() { return city; } public String toString() { return "[PartData: name=" + name + " color=" + color + " weight=" + weight + " city=" + city + ']'; } }
简单的实体和key,与json有些类似。
SampleDatabase.java,建立数据库并创建日志
import com.sleepycat.bind.serial.StoredClassCatalog; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.DatabaseException; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; import java.io.File; import java.io.FileNotFoundException; public class SampleDatabase { private Environment env; private static final String CLASS_CATALOG = "java_class_catalog"; private StoredClassCatalog javaCatalog; private static final String PART_STORE = "part_store"; private Database partDb; public SampleDatabase(String homeDirectory) throws DatabaseException, FileNotFoundException { System.out.println("Opening environment in: " + homeDirectory); EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setTransactional(true); envConfig.setAllowCreate(true); env = new Environment(new File(homeDirectory), envConfig); DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); partDb = env.openDatabase(null, PART_STORE, dbConfig); Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig); javaCatalog = new StoredClassCatalog(catalogDb); } public void close() throws DatabaseException { partDb.close(); javaCatalog.close(); env.close(); } public final Environment getEnvironment() { return env; } public final Database getPartDatabase() { return partDb; } public StoredClassCatalog getClassCatalog() { return javaCatalog; } }
SampleViews.java,将key-value绑定到数据库
import berkeley.entity.PartData; import berkeley.entity.PartKey; import com.sleepycat.bind.EntryBinding; import com.sleepycat.bind.serial.ClassCatalog; import com.sleepycat.bind.serial.SerialBinding; import com.sleepycat.collections.StoredEntrySet; import com.sleepycat.collections.StoredMap; public class SampleViews { private StoredMap partMap; public SampleViews(SampleDatabase db) { ClassCatalog catalog = db.getClassCatalog(); EntryBinding partKeyBinding = new SerialBinding(catalog, PartKey.class); EntryBinding partValueBinding = new SerialBinding(catalog, PartData.class); partMap = new StoredMap(db.getPartDatabase(), partKeyBinding, partValueBinding, true); } public final StoredMap getPartMap() { return partMap; } public final StoredEntrySet getPartEntrySet() { return (StoredEntrySet) partMap.entrySet(); } }
Sample.java
import java.io.FileNotFoundException; import java.util.Iterator; import java.util.Map; import berkeley.db.SampleDatabase; import berkeley.db.SampleViews; import berkeley.entity.PartData; import berkeley.entity.PartKey; import berkeley.entity.Weight; import com.sleepycat.collections.TransactionRunner; import com.sleepycat.collections.TransactionWorker; import com.sleepycat.je.DatabaseException; public class Sample { private SampleDatabase db; private SampleViews views; private Sample(String homeDir) throws DatabaseException, FileNotFoundException { db = new SampleDatabase(homeDir); views = new SampleViews(db); } private void close() throws DatabaseException{ db.close(); } private void run() throws Exception { TransactionRunner runner = new TransactionRunner(db.getEnvironment()); runner.run(new PopulateDatabase()); runner.run(new PrintDatabase()); } private class PrintDatabase implements TransactionWorker { public void doWork() throws Exception{ addParts(); } private void addParts() { Map parts = views.getPartMap(); if (parts.isEmpty()) { System.out.println("Adding Parts"); parts.put(new PartKey("P1"), new PartData("Nut", "Red", new Weight(12.0, Weight.GRAMS), "London")); parts.put(new PartKey("P2"), new PartData("Bolt", "Green", new Weight(17.0, Weight.GRAMS), "Paris")); parts.put(new PartKey("P3"), new PartData("Screw", "Blue", new Weight(17.0, Weight.GRAMS), "Rome")); parts.put(new PartKey("P4"), new PartData("Screw", "Red", new Weight(14.0, Weight.GRAMS), "London")); parts.put(new PartKey("P5"), new PartData("Cam", "Blue", new Weight(12.0, Weight.GRAMS), "Paris")); parts.put(new PartKey("P6"), new PartData("Cog", "Red", new Weight(19.0, Weight.GRAMS), "London")); } } } private class PopulateDatabase implements TransactionWorker { public void doWork() throws Exception { printEntries("Parts", views.getPartEntrySet().iterator()); } private void printEntries(String label, Iterator iterator) { System.out.println("/n--- " + label + " ---"); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); System.out.println(entry.getKey().toString()); System.out.println(entry.getValue().toString()); } } } public PartData getParts() { return (PartData) views.getPartMap().get(new PartKey("P6")); } public static void main(String[] args) { Sample sample = null; try { System.out.println("/nRunning sample: " + Sample.class); String homeDir = "C:/tmp"; sample = new Sample(homeDir); sample.run(); System.out.println(sample.getParts()); } catch (Exception e) { e.printStackTrace(); } finally { if (sample != null) { try { sample.close(); } catch (Exception e) { System.err.println("Exception during database close:"); e.printStackTrace(); } } } } }
运行结果:
Running sample: class berkeley.Sample
Opening environment in: C:/tmp
--- Parts ---
[PartKey: number=P1]
[PartData: name=Nut color=Red weight=[12.0 grams] city=London]
[PartKey: number=P2]
[PartData: name=Bolt color=Green weight=[17.0 grams] city=Paris]
[PartKey: number=P3]
[PartData: name=Screw color=Blue weight=[17.0 grams] city=Rome]
[PartKey: number=P4]
[PartData: name=Screw color=Red weight=[14.0 grams] city=London]
[PartKey: number=P5]
[PartData: name=Cam color=Blue weight=[12.0 grams] city=Paris]
[PartKey: number=P6]
[PartData: name=Cog color=Red weight=[19.0 grams] city=London]
[PartData: name=Cog color=Red weight=[19.0 grams] city=London]
再把 sample.run() 注释掉看看:
[PartData: name=Cog color=Red weight=[19.0 grams] city=London]