初始化
RocksDB.loadLibrary();//加载jni
RocksDB db = RocksDB.open(options, db_path_not_found)//打开数据库
使用
单条插入
db.put("hello".getBytes(), "world".getBytes());
批量插入
try (final WriteOptions writeOpt = new WriteOptions()) {
for (int i = 10; i <= 19; ++i) {
try (final WriteBatch batch = new WriteBatch()) {
for (int j = 10; j <= 19; ++j) {
batch.put(String.format("%dx%d", i, j).getBytes(),
String.format("%d", i * j).getBytes());
}
db.write(writeOpt, batch);
}
}
查询
value = db.get("world".getBytes());
迭代器查询
try (final RocksIterator iterator = db.newIterator()) {
for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) {
values.add(iterator.value ());
}
}
转为java代码就可以了
auto iter = db->NewIterator(ReadOptions());
Slice prefix = options.prefix_extractor->Transform("key0");
for (iter->Seek("key0"); iter->Valid() && iter->key().starts_with(prefix); iter->Next())
{
std::cout << iter->key().ToString() << ": " << iter->value().ToString() << std::endl;
}
转载地址:
https://blog.csdn.net/gunri_tianjin/article/details/83444651