注:本文翻译自https://github.com/fusesource/leveldbjni
LevelDB API的使用
1.导包命令:
import org.iq80.leveldb.*;
import static org.fusesource.leveldbjni.JniDBFactory.*;
import java.io.*;
2.关闭开启数据库
Options options = new Options();
options.createIfMissing(true);
DB db = factory.open(new File(\"example\"), options);
try {
// Use the db in here....
} finally {
// Make sure you close the db to shutdown the
// data and avoid resource leaks.
db.close();
}
3.增、查、删数据
db.put(bytes(\"Tampa\"), bytes(\"rocks\"));
String value = asString(db.get(bytes(\"Tampa\")));
db.delete(bytes(\"Tampa\"));
4.批量操作,Performing Batch/Bulk/Atomic Updates.
WriteBatch batch = db.createWriteBatch();
try {
batch.delete(bytes(\"Denver\"));
batch.put(bytes(\"Tampa\"), bytes(\"green\"));
batch.put(bytes(\"London\"), bytes(\"red\"));
db.write(batch);
} finally {
// Make sure you close the batch to avoid resource leaks.
batch.close();
}
5. 遍历 Iterating key/values.
DBIterator iterator = db.iterator();
try {
for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
String key = asString(iterator.peekNext().getKey());
String value = asString(iterator.peekNext().getValue());
System.out.println(key+\" = \"+value);
}
} finally {
// Make sure you close the iterator to avoid resource leaks.
iterator.close();
}
6. 快照 Working against a Snapshot view of the Data .
ReadOptions ro = new ReadOptions();
ro.snapshot(db.getSnapshot());
try {
// All read operations will now use the same
// consistent view of the data.
... = db.iterator(ro);
... = db.get(bytes(\"Tampa\"), ro);
} finally {
// Make sure you close the snapshot to avoid resource leaks.
ro.snapshot().close();
}
7. 自定义比较器 Using a custom Comparator.
DBComparator comparator = new DBComparator(){
public int compare(byte[] key1, byte[] key2) {
return new String(key1).compareTo(new String(key2));
}
public String name() {
return \"simple\";
}
public byte[] findShortestSeparator(byte[] start, byte[] limit) {
return start;
}
public byte[] findShortSuccessor(byte[] key) {
return key;
}
};
Options options = new Options();
options.comparator(comparator);
DB db = factory.open(new File(\"example\"), options);
8. 禁用压缩 Disabling Compression
Options options = new Options();
options.compressionType(CompressionType.NONE);
DB db = factory.open(new File(\"example\"), options);
9.配置缓存 Configuring the Cache
Options options = new Options();
options.cacheSize(100 * 1048576); // 100MB cache
DB db = factory.open(new File(\"example\"), options);
9.获得近似尺寸 Getting approximate sizes.
long[] sizes = db.getApproximateSizes(new Range(bytes(\"a\"), bytes(\"k\")), new Range(bytes(\"k\"), bytes(\"z\")));
System.out.println(\"Size: \"+sizes[0]+\", \"+sizes[1]);
10.获取数据库状态 Getting data status.
String stats = db.getProperty(\"leveldb.stats\");
System.out.println(stats);
11.获取日志信息 Getting informational log messages.
Logger logger = new Logger() {
public void log(String message) {
System.out.println(message);
}
};
Options options = new Options();
options.logger(logger);
DB db = factory.open(new File(\"example\"), options);
12.销毁数据库 Destroying a data .
Options options = new Options();
factory.destroy(new File(\"example\"), options);
13.修复数据库 Repairing a data .
Options options = new Options();
factory.repair(new File(\"example\"), options);
14使用内存池可以提高本机内存分配的效率: Using a memory pool to make native memory allocations more efficient:
JniDBFactory.pushMemoryPool(1024 * 512);
try {
// .. work with the DB in here,
} finally {
JniDBFactory.popMemoryPool();
}
继续阅读与本文标签相同的文章
下一篇 :
大咖来信 | 李开复:美国AI见闻录
-
东京车展丰田玩把大的 人工智能自动驾驶都有 新RAV4也将混动亮相
2026-05-18栏目: 教程
-
“北斗+”为长沙公共安全“站岗”
2026-05-18栏目: 教程
-
滴滴与清华大学成立未来出行联合研究中心
2026-05-18栏目: 教程
-
三星S10被曝屏下指纹存在安全漏洞:任何人都还可以解锁
2026-05-18栏目: 教程
-
进博会“催生”上海首个保税展示展销场所
2026-05-18栏目: 教程
