欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java RocksDB安装与应用

程序员文章站 2023-12-15 15:34:16
rocksdb 是一个可嵌入的,持久性的 key-value存储。 以下介绍来自rocksdb 中文官网  https://rocksdb.org.cn/...

rocksdb 是一个可嵌入的,持久性的 key-value存储。

以下介绍来自rocksdb 中文官网 

https://rocksdb.org.cn/

它有以下四个特点

1 高性能:rocksdb使用一套日志结构的数据库引擎,为了更好的性能,这套引擎是用c++编写的。 key和value是任意大小的字节流。

2 为快速存储而优化:rocksdb为快速而又低延迟的存储设备(例如闪存或者高速硬盘)而特殊优化处理。 rocksdb将最大限度的发挥闪存和ram的高度率读写性能。

3 可适配性 :rocksdb适合于多种不同工作量类型。 从像myrocks这样的数据存储引擎, 到应用数据缓存, 甚至是一些嵌入式工作量,rocksdb都可以从容面对这些不同的数据工作量需求。

4 基础和高级的数据库操作  rocksdb提供了一些基础的操作,例如打开和关闭数据库。 对于合并和压缩过滤等高级操作,也提供了读写支持。

​​​​​​rockdb 安装与使用

rocksdb 安装有多种方式。由于官方没有提供对应平台的二进制库,所以需要自己编译使用。

rocksdb 的安装很简单,但是需要转变一下对于rocksdb 的看法。它不是一个重量级别的数据库,是一个嵌入式的key-value 存储。这意味着你只要在你的maven项目中添加 rocksdb的依赖,就可以在开发环境中自我尝试了。如果你没有理解这点,你就可能会走入下面这两种不推荐的安装方式。

方式 一   去查看rocksdb 的官网 发现要写 一个c++ 程序(不推荐)

#include <assert>
#include "rocksdb/db.h"
rocksdb::db* db;
rocksdb::options options;
options.create_if_missing = true;
rocksdb::status status =
 rocksdb::db::open(options, "/tmp/testdb", &db);
assert(status.ok());

创建一个数据库???? 怎么和之前用的mysql 或者mongo 不一样,为啥没有一个start.sh 或者start.bat 之类的脚本。难道要我写。写完了编译发现还不知道怎么和rocksdb 库进行关联,怎么办,我c++都忘完了。

方式二  使用pyrocksdb (不推荐)

http://pyrocksdb.readthedocs.io/en/latest/installation.html

详细的安装文档见pyrocksdb 的官网安装文档。

以上两种方式对于熟悉c++ 或者python 的开发者来说都比较友好,但对于java 开发者来说不是太友好。

接下来就介绍第三种方式。

方式三 使用maven (推荐)

新建maven 项目,修改pom.xml 依赖里面添加

<dependency>
 <groupid>org.rocksdb</groupid>
 <artifactid>rocksdbjni</artifactid>
 <version>5.8.6</version>
</dependency>

可以选择你喜欢的版本。

然后更高maven 的语言级别,我这里全局设置为了1.8

<profiles>
 <profile>
 <id>jdk18</id>
 <activation>
  <activebydefault>true</activebydefault>
  <jdk>1.8</jdk>
 </activation>
 <properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.compilerversion>1.8</maven.compiler.compilerversion>
 </properties>
 </profile>
</profiles>

到这里,环境就装好了,是不是又回到了熟悉的java 世界。

然后copy 源码包下的一个类,在ide中修改一下运行配置,加一个程序运行中数据库存储路径,就可以运行测试了 。我会在文章最后给出这个类。

Java RocksDB安装与应用

运行控制台会有日志输出,同时也文件中也会出现一下新的文件。

Java RocksDB安装与应用

Java RocksDB安装与应用

后面会更新更多关于rockdb 开发api 的介绍,以及在生产中的应用,希望大家关注。

// copyright (c) 2011-present, facebook, inc. all rights reserved.
// this source code is licensed under both the gplv2 (found in the
// copying file in the root directory) and apache 2.0 license
// (found in the license.apache file in the root directory).

import org.rocksdb.*;
import org.rocksdb.util.sizeunit;
import java.util.arraylist;
import java.util.arrays;
import java.util.list;
import java.util.map;
public class rocksdbsample {
 static {
 rocksdb.loadlibrary();
 }
 public static void main(final string[] args) {
 if (args.length < 1) {
 system.out.println("usage: rocksdbsample db_path");
 system.exit(-1);
 }
 final string db_path = args[0];
 final string db_path_not_found = db_path + "_not_found";
 system.out.println("rocksdbsample");
 try (final options options = new options();
  final filter bloomfilter = new bloomfilter(10);
  final readoptions readoptions = new readoptions()
  .setfillcache(false);
  final statistics stats = new statistics();
  final ratelimiter ratelimiter = new ratelimiter(10000000,10000, 10)) {
 try (final rocksdb db = rocksdb.open(options, db_path_not_found)) {
 assert (false);
 } catch (final rocksdbexception e) {
 system.out.format("caught the expected exception -- %s\n", e);
 }
 try {
 options.setcreateifmissing(true)
  .setstatistics(stats)
  .setwritebuffersize(8 * sizeunit.kb)
  .setmaxwritebuffernumber(3)
  .setmaxbackgroundcompactions(10)
  .setcompressiontype(compressiontype.snappy_compression)
  .setcompactionstyle(compactionstyle.universal);
 } catch (final illegalargumentexception e) {
 assert (false);
 }
 assert (options.createifmissing() == true);
 assert (options.writebuffersize() == 8 * sizeunit.kb);
 assert (options.maxwritebuffernumber() == 3);
 assert (options.maxbackgroundcompactions() == 10);
 assert (options.compressiontype() == compressiontype.snappy_compression);
 assert (options.compactionstyle() == compactionstyle.universal);
 assert (options.memtablefactoryname().equals("skiplistfactory"));
 options.setmemtableconfig(
  new hashskiplistmemtableconfig()
  .setheight(4)
  .setbranchingfactor(4)
  .setbucketcount(2000000));
 assert (options.memtablefactoryname().equals("hashskiplistrepfactory"));
 options.setmemtableconfig(
  new hashlinkedlistmemtableconfig()
  .setbucketcount(100000));
 assert (options.memtablefactoryname().equals("hashlinkedlistrepfactory"));
 options.setmemtableconfig(
  new vectormemtableconfig().setreservedsize(10000));
 assert (options.memtablefactoryname().equals("vectorrepfactory"));
 options.setmemtableconfig(new skiplistmemtableconfig());
 assert (options.memtablefactoryname().equals("skiplistfactory"));
 options.settableformatconfig(new plaintableconfig());
 // plain-table requires mmap read
 options.setallowmmapreads(true);
 assert (options.tablefactoryname().equals("plaintable"));
 options.setratelimiter(ratelimiter);
 final blockbasedtableconfig table_options = new blockbasedtableconfig();
 table_options.setblockcachesize(64 * sizeunit.kb)
  .setfilter(bloomfilter)
  .setcachenumshardbits(6)
  .setblocksizedeviation(5)
  .setblockrestartinterval(10)
  .setcacheindexandfilterblocks(true)
  .sethashindexallowcollision(false)
  .setblockcachecompressedsize(64 * sizeunit.kb)
  .setblockcachecompressednumshardbits(10);
 assert (table_options.blockcachesize() == 64 * sizeunit.kb);
 assert (table_options.cachenumshardbits() == 6);
 assert (table_options.blocksizedeviation() == 5);
 assert (table_options.blockrestartinterval() == 10);
 assert (table_options.cacheindexandfilterblocks() == true);
 assert (table_options.hashindexallowcollision() == false);
 assert (table_options.blockcachecompressedsize() == 64 * sizeunit.kb);
 assert (table_options.blockcachecompressednumshardbits() == 10);
 options.settableformatconfig(table_options);
 assert (options.tablefactoryname().equals("blockbasedtable"));
 try (final rocksdb db = rocksdb.open(options, db_path)) {
 db.put("hello".getbytes(), "world".getbytes());
 final byte[] value = db.get("hello".getbytes());
 assert ("world".equals(new string(value)));
 final string str = db.getproperty("rocksdb.stats");
 assert (str != null && !str.equals(""));
 } catch (final rocksdbexception e) {
 system.out.format("[error] caught the unexpected exception -- %s\n", e);
 assert (false);
 }
 try (final rocksdb db = rocksdb.open(options, db_path)) {
 db.put("hello".getbytes(), "world".getbytes());
 byte[] value = db.get("hello".getbytes());
 system.out.format("get('hello') = %s\n",
  new string(value));
 for (int i = 1; i <= 9; ++i) {
  for (int j = 1; j <= 9; ++j) {
  db.put(string.format("%dx%d", i, j).getbytes(),
  string.format("%d", i * j).getbytes());
  }
 }
 for (int i = 1; i <= 9; ++i) {
  for (int j = 1; j <= 9; ++j) {
  system.out.format("%s ", new string(db.get(
  string.format("%dx%d", i, j).getbytes())));
  }
  system.out.println("");
 }
 // write batch test
 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);
  }
  }
 }
 for (int i = 10; i <= 19; ++i) {
  for (int j = 10; j <= 19; ++j) {
  assert (new string(
  db.get(string.format("%dx%d", i, j).getbytes())).equals(
  string.format("%d", i * j)));
  system.out.format("%s ", new string(db.get(
  string.format("%dx%d", i, j).getbytes())));
  }
  system.out.println("");
 }
 value = db.get("1x1".getbytes());
 assert (value != null);
 value = db.get("world".getbytes());
 assert (value == null);
 value = db.get(readoptions, "world".getbytes());
 assert (value == null);
 final byte[] testkey = "asdf".getbytes();
 final byte[] testvalue =
  "asdfghjkl;'?><mnbvcxzqwertyuiop{+_)(*&^%$#@".getbytes();
 db.put(testkey, testvalue);
 byte[] testresult = db.get(testkey);
 assert (testresult != null);
 assert (arrays.equals(testvalue, testresult));
 assert (new string(testvalue).equals(new string(testresult)));
 testresult = db.get(readoptions, testkey);
 assert (testresult != null);
 assert (arrays.equals(testvalue, testresult));
 assert (new string(testvalue).equals(new string(testresult)));
 final byte[] insufficientarray = new byte[10];
 final byte[] enougharray = new byte[50];
 int len;
 len = db.get(testkey, insufficientarray);
 assert (len > insufficientarray.length);
 len = db.get("asdfjkl;".getbytes(), enougharray);
 assert (len == rocksdb.not_found);
 len = db.get(testkey, enougharray);
 assert (len == testvalue.length);
 len = db.get(readoptions, testkey, insufficientarray);
 assert (len > insufficientarray.length);
 len = db.get(readoptions, "asdfjkl;".getbytes(), enougharray);
 assert (len == rocksdb.not_found);
 len = db.get(readoptions, testkey, enougharray);
 assert (len == testvalue.length);
 db.remove(testkey);
 len = db.get(testkey, enougharray);
 assert (len == rocksdb.not_found);
 // repeat the test with writeoptions
 try (final writeoptions writeopts = new writeoptions()) {
  writeopts.setsync(true);
  writeopts.setdisablewal(true);
  db.put(writeopts, testkey, testvalue);
  len = db.get(testkey, enougharray);
  assert (len == testvalue.length);
  assert (new string(testvalue).equals(
  new string(enougharray, 0, len)));
 }
 try {
  for (final tickertype statstype : tickertype.values()) {
  if (statstype != tickertype.ticker_enum_max) {
  stats.gettickercount(statstype);
  }
  }
  system.out.println("gettickercount() passed.");
 } catch (final exception e) {
  system.out.println("failed in call to gettickercount()");
  assert (false); //should never reach here.
 }
 try {
  for (final histogramtype histogramtype : histogramtype.values()) {
  if (histogramtype != histogramtype.histogram_enum_max) {
  histogramdata data = stats.gethistogramdata(histogramtype);
  }
  }
  system.out.println("gethistogramdata() passed.");
 } catch (final exception e) {
  system.out.println("failed in call to gethistogramdata()");
  assert (false); //should never reach here.
 }
 try (final rocksiterator iterator = db.newiterator()) {
  boolean seektofirstpassed = false;
  for (iterator.seektofirst(); iterator.isvalid(); iterator.next()) {
  iterator.status();
  assert (iterator.key() != null);
  assert (iterator.value() != null);
  seektofirstpassed = true;
  }
  if (seektofirstpassed) {
  system.out.println("iterator seektofirst tests passed.");
  }
  boolean seektolastpassed = false;
  for (iterator.seektolast(); iterator.isvalid(); iterator.prev()) {
  iterator.status();
  assert (iterator.key() != null);
  assert (iterator.value() != null);
  seektolastpassed = true;
  }
  if (seektolastpassed) {
  system.out.println("iterator seektolastpassed tests passed.");
  }
  iterator.seektofirst();
  iterator.seek(iterator.key());
  assert (iterator.key() != null);
  assert (iterator.value() != null);
  system.out.println("iterator seek test passed.");
 }
 system.out.println("iterator tests passed.");
 final list<byte[]> keys = new arraylist<>();
 try (final rocksiterator iterator = db.newiterator()) {
  for (iterator.seektolast(); iterator.isvalid(); iterator.prev()) {
  keys.add(iterator.key());
  }
 }
 map<byte[], byte[]> values = db.multiget(keys);
 assert (values.size() == keys.size());
 for (final byte[] value1 : values.values()) {
  assert (value1 != null);
 }
 values = db.multiget(new readoptions(), keys);
 assert (values.size() == keys.size());
 for (final byte[] value1 : values.values()) {
  assert (value1 != null);
 }
 } catch (final rocksdbexception e) {
 system.err.println(e);
 }
 }
 }
}

以上就是本次给大家介绍的java中rocksdb安装与应用的全部内容,如果大家在学习后还有任何不明白的可以在下方的留言区域讨论,感谢对的支持。

上一篇:

下一篇: