Java RocksDB安裝與應(yīng)用
rocksDB 是一個(gè)可嵌入的,持久性的 key-value存儲(chǔ)。
以下介紹來(lái)自rocksDB 中文官網(wǎng)
https://rocksdb.org.cn/
它有以下四個(gè)特點(diǎn)
1 高性能:RocksDB使用一套日志結(jié)構(gòu)的數(shù)據(jù)庫(kù)引擎,為了更好的性能,這套引擎是用C++編寫的。 Key和value是任意大小的字節(jié)流。
2 為快速存儲(chǔ)而優(yōu)化:RocksDB為快速而又低延遲的存儲(chǔ)設(shè)備(例如閃存或者高速硬盤)而特殊優(yōu)化處理。 RocksDB將最大限度的發(fā)揮閃存和RAM的高度率讀寫性能。
3 可適配性 :RocksDB適合于多種不同工作量類型。 從像MyRocks這樣的數(shù)據(jù)存儲(chǔ)引擎, 到應(yīng)用數(shù)據(jù)緩存, 甚至是一些嵌入式工作量,RocksDB都可以從容面對(duì)這些不同的數(shù)據(jù)工作量需求。
4 基礎(chǔ)和高級(jí)的數(shù)據(jù)庫(kù)操作 RocksDB提供了一些基礎(chǔ)的操作,例如打開和關(guān)閉數(shù)據(jù)庫(kù)。 對(duì)于合并和壓縮過(guò)濾等高級(jí)操作,也提供了讀寫支持。
RockDB 安裝與使用
rocksDB 安裝有多種方式。由于官方?jīng)]有提供對(duì)應(yīng)平臺(tái)的二進(jìn)制庫(kù),所以需要自己編譯使用。
rocksDB 的安裝很簡(jiǎn)單,但是需要轉(zhuǎn)變一下對(duì)于rocksDB 的看法。它不是一個(gè)重量級(jí)別的數(shù)據(jù)庫(kù),是一個(gè)嵌入式的key-value 存儲(chǔ)。這意味著你只要在你的Maven項(xiàng)目中添加 rocksDB的依賴,就可以在開發(fā)環(huán)境中自我嘗試了。如果你沒有理解這點(diǎn),你就可能會(huì)走入下面這兩種不推薦的安裝方式。
方式 一 去查看rocksDB 的官網(wǎng) 發(fā)現(xiàn)要寫 一個(gè)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());
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)???? 怎么和之前用的mysql 或者mongo 不一樣,為啥沒有一個(gè)start.sh 或者start.bat 之類的腳本。難道要我寫。寫完了編譯發(fā)現(xiàn)還不知道怎么和rocksDB 庫(kù)進(jìn)行關(guān)聯(lián),怎么辦,我C++都忘完了。
方式二 使用pyrocksDB (不推薦)
http://pyrocksdb.readthedocs.io/en/latest/installation.html
詳細(xì)的安裝文檔見pyrocksDB 的官網(wǎng)安裝文檔。
以上兩種方式對(duì)于熟悉C++ 或者python 的開發(fā)者來(lái)說(shuō)都比較友好,但對(duì)于java 開發(fā)者來(lái)說(shuō)不是太友好。
接下來(lái)就介紹第三種方式。
方式三 使用maven (推薦)
新建maven 項(xiàng)目,修改pom.xml 依賴?yán)锩嫣砑?/p>
<dependency> <groupId>org.rocksdb</groupId> <artifactId>rocksdbjni</artifactId> <version>5.8.6</version> </dependency>
可以選擇你喜歡的版本。
然后更高maven 的語(yǔ)言級(jí)別,我這里全局設(shè)置為了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>
到這里,環(huán)境就裝好了,是不是又回到了熟悉的java 世界。
然后copy 源碼包下的一個(gè)類,在IDE中修改一下運(yùn)行配置,加一個(gè)程序運(yùn)行中數(shù)據(jù)庫(kù)存儲(chǔ)路徑,就可以運(yùn)行測(cè)試了 。我會(huì)在文章最后給出這個(gè)類。

運(yùn)行控制臺(tái)會(huì)有日志輸出,同時(shí)也文件中也會(huì)出現(xiàn)一下新的文件。


后面會(huì)更新更多關(guān)于rockDB 開發(fā)API 的介紹,以及在生產(chǎn)中的應(yīng)用,希望大家關(guān)注。
// 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安裝與應(yīng)用的全部?jī)?nèi)容,如果大家在學(xué)習(xí)后還有任何不明白的可以在下方的留言區(qū)域討論,感謝對(duì)腳本之家的支持。
相關(guān)文章
Java棧之鏈?zhǔn)綏4鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java棧之鏈?zhǔn)綏4鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
基于SpringBoot項(xiàng)目遇到的坑--Date入?yún)?wèn)題
這篇文章主要介紹了SpringBoot項(xiàng)目遇到的坑--Date入?yún)?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
logback-spring.xml的內(nèi)容格式詳解
這篇文章主要介紹了logback-spring.xml的內(nèi)容格式詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的的朋友參考下吧2023-11-11
JDK1.6“新“特性Instrumentation之JavaAgent(推薦)
這篇文章主要介紹了JDK1.6“新“特性Instrumentation之JavaAgent,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Java實(shí)現(xiàn)儲(chǔ)存對(duì)象并按對(duì)象某屬性排序的幾種方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)儲(chǔ)存對(duì)象并按對(duì)象某屬性排序的幾種方法,結(jié)合實(shí)例形式詳細(xì)分析了Java儲(chǔ)存對(duì)象并按對(duì)象某屬性排序的具體實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
SpringBoot中的CompletableFuture類詳解
這篇文章主要介紹了SpringBoot中的CompletableFuture類詳解,在?Java8中,引入了CompletableFuture類,它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)執(zhí)行異步任務(wù),今天我們就來(lái)詳細(xì)解讀一下這個(gè)類,需要的朋友可以參考下2023-07-07
Java 實(shí)戰(zhàn)項(xiàng)目之疫情防控管理系統(tǒng)詳解
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)疫情防控管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11

