JavaRocksDB安装与应用

更新时间:2023-06-04 10:23:02 阅读: 评论:0

JavaRocksDB安装与应⽤
rocksDB 是⼀个可嵌⼊的,持久性的 key-value存储。小学科学教学
以下介绍来⾃rocksDB 中⽂官⽹
它有以下四个特点
1 ⾼性能:RocksDB使⽤⼀套⽇志结构的数据库引擎,为了更好的性能,这套引擎是⽤C++编写的。 Key和value是任意⼤⼩的字节流。
2 为快速存储⽽优化:RocksDB为快速⽽⼜低延迟的存储设备(例如闪存或者⾼速硬盘)⽽特殊优化处理。 RocksDB将最⼤限度的发挥闪存和RAM的⾼度率读写性能。
3 可适配性:RocksDB适合于多种不同⼯作量类型。从像MyRocks这样的数据存储引擎,到应⽤数据缓存, 甚⾄是⼀些嵌⼊式⼯作量,RocksDB都可以从容⾯对这些不同的数据⼯作量需求。
4 基础和⾼级的数据库操作  RocksDB提供了⼀些基础的操作,例如打开和关闭数据库。对于合并和压缩过滤等⾼级操作,也提供了读写⽀持。
R ockDB 安装与使⽤
rocksDB 安装有多种⽅式。由于官⽅没有提供对应平台的⼆进制库,所以需要⾃⼰编译使⽤。
rocksDB 的安装很简单,但是需要转变⼀下对于rocksDB 的看法。它不是⼀个重量级别的数据库,是⼀个嵌⼊式的key-value 存储。这意味着你只要在你的Maven项⽬中添加 rocksDB的依赖,就可以在开发环境中⾃我尝试了。如果你没有理解这点,你就可能会⾛⼊下⾯这两种不推荐的安装⽅式。
⽅式⼀去查看rocksDB 的官⽹发现要写⼀个C++ 程序(不推荐)
札幌拉面#include <asrt>
#include "rocksdb/db.h"
rocksdb::DB* db;
rocksdb::Options options;
rocksdb::Status status =
rocksdb::DB::Open(options, "/tmp/testdb", &db);
asrt(status.ok());
创建⼀个数据库怎么和之前⽤的mysql 或者mongo 不⼀样,为啥没有⼀个start.sh 或者start.bat 之类的脚本。难道要我写。写完了编译发现还不知道怎么和rocksDB 库进⾏关联,怎么办,我C++都忘完了。
⽅式⼆使⽤pyrocksDB (不推荐)
详细的安装⽂档见pyrocksDB 的官⽹安装⽂档。
以上两种⽅式对于熟悉C++ 或者python 的开发者来说都⽐较友好,但对于java 开发者来说不是太友好。
接下来就介绍第三种⽅式。
⽅式三使⽤maven (推荐)
新建maven 项⽬,修改l 依赖⾥⾯添加
<dependency>
<groupId&ksdb</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>
&piler.source>1.8</piler.source>
&piler.target>1.8</piler.target>
&pilerVersion>1.8</pilerVersion>
</properties>
</profile>
</profiles>
到这⾥,环境就装好了,是不是⼜回到了熟悉的java 世界。
然后copy 源码包下的⼀个类,在IDE中修改⼀下运⾏配置,加⼀个程序运⾏中数据库存储路径,就可以运⾏测试了。我会在⽂章最后给出这个类。
运⾏控制台会有⽇志输出,同时也⽂件中也会出现⼀下新的⽂件。
后⾯会更新更多关于rockDB 开发API 的介绍,以及在⽣产中的应⽤,希望⼤家关注。
// Copyright (c) 2011-prent, Facebook, Inc. All rights rerved.
// This source code is licend under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 Licen
// (found in the LICENSE.Apache file in the root directory).
ksdb.*;
ksdb.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");
}
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()
.tFillCache(fal);
final Statistics stats = new Statistics();
final RateLimiter rateLimiter = new RateLimiter(10000000,10000, 10)) {
try (final RocksDB db = RocksDB.open(options, db_path_not_found)) {
asrt (fal);
} catch (final RocksDBException e) {
System.out.format("Caught the expected exception -- %s\n", e);
}
try {
options.tCreateIfMissing(true)
.tStatistics(stats)
.tWriteBufferSize(8 * SizeUnit.KB)
.tMaxWriteBufferNumber(3)
.tMaxBackgroundCompactions(10)
.tCompressionType(CompressionType.SNAPPY_COMPRESSION)
.tCompactionStyle(CompactionStyle.UNIVERSAL);
} catch (final IllegalArgumentException e) {
asrt (fal);
}
asrt (ateIfMissing() == true);
asrt (options.writeBufferSize() == 8 * SizeUnit.KB);
asrt (options.maxWriteBufferNumber() == 3);
asrt (options.maxBackgroundCompactions() == 10);
asrt (pressionType() == CompressionType.SNAPPY_COMPRESSION); asrt (pactionStyle() == CompactionStyle.UNIVERSAL);
asrt (TableFactoryName().equals("SkipListFactory"));
options.tMemTableConfig(
new HashSkipListMemTableConfig()
.tHeight(4)
.tBranchingFactor(4)
.tBucketCount(2000000));
asrt (TableFactoryName().equals("HashSkipListRepFactory"));
options.tMemTableConfig(
new HashLinkedListMemTableConfig()
.tBucketCount(100000));
asrt (TableFactoryName().equals("HashLinkedListRepFactory"));
options.tMemTableConfig(
new VectorMemTableConfig().tRervedSize(10000));
asrt (TableFactoryName().equals("VectorRepFactory"));
options.tMemTableConfig(new SkipListMemTableConfig());
asrt (TableFactoryName().equals("SkipListFactory"));
options.tTableFormatConfig(new PlainTableConfig());
// Plain-Table requires mmap read
options.tAllowMmapReads(true);
asrt (options.tableFactoryName().equals("PlainTable"));
options.tRateLimiter(rateLimiter);
final BlockBadTableConfig table_options = new BlockBadTableConfig();
table_options.tBlockCacheSize(64 * SizeUnit.KB)
.tFilter(bloomFilter)
.tCacheNumShardBits(6)
.tBlockSizeDeviation(5)
.tBlockRestartInterval(10)
.
tCacheIndexAndFilterBlocks(true)
.tHashIndexAllowCollision(fal)
.tBlockCacheCompresdSize(64 * SizeUnit.KB)
.tBlockCacheCompresdNumShardBits(10);
asrt (table_options.blockCacheSize() == 64 * SizeUnit.KB);
asrt (table_options.cacheNumShardBits() == 6);
asrt (table_options.blockSizeDeviation() == 5);
asrt (table_options.blockRestartInterval() == 10);
asrt (table_options.cacheIndexAndFilterBlocks() == true);
asrt (table_options.hashIndexAllowCollision() == fal);
asrt (table_options.blockCacheCompresdSize() == 64 * SizeUnit.KB);
asrt (table_options.blockCacheCompresdNumShardBits() == 10);
options.tTableFormatConfig(table_options);
asrt (options.tableFactoryName().equals("BlockBadTable"));
try (final RocksDB db = RocksDB.open(options, db_path)) {
烟台蓬莱阁db.put("hello".getBytes(), "world".getBytes());
final byte[] value = db.get("hello".getBytes());
asrt ("world".equals(new String(value)));
final String str = db.getProperty("rocksdb.stats");
asrt (str != null && !str.equals(""));
} catch (final RocksDBException e) {
System.out.format("[ERROR] caught the unexpected exception -- %s\n", e);
asrt (fal);
}
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.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) {
asrt (new String(
<(String.format("%dx%d", i, j).getBytes())).equals(
String.format("%d", i * j)));
System.out.format("%s ", new (
String.format("%dx%d", i, j).getBytes())));
}
System.out.println("");
}
value = db.get("1x1".getBytes());
asrt (value != null);
value = db.get("world".getBytes());
asrt (value == null);
value = db.get(readOptions, "world".getBytes());
asrt (value == null);
final byte[] testKey = "asdf".getBytes();
final byte[] testValue =
"asdfghjkl;'?><MNBVCXZQWERTYUIOP{+_)(*&^%$#@".getBytes(); db.put(testKey, testValue);
byte[] testResult = db.get(testKey);
asrt (testResult != null);
asrt (Arrays.equals(testValue, testResult));
asrt (new String(testValue).equals(new String(testResult)));
testResult = db.get(readOptions, testKey);
asrt (testResult != null);
asrt (Arrays.equals(testValue, testResult));
asrt (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);
asrt (len > insufficientArray.length);
dm宣传单len = db.get("asdfjkl;".getBytes(), enoughArray);
asrt (len == RocksDB.NOT_FOUND);
len = db.get(testKey, enoughArray);
asrt (len == testValue.length);
len = db.get(readOptions, testKey, insufficientArray);
asrt (len > insufficientArray.length);
len = db.get(readOptions, "asdfjkl;".getBytes(), enoughArray);
asrt (len == RocksDB.NOT_FOUND);
len = db.get(readOptions, testKey, enoughArray);
asrt (len == testValue.length);
len = db.get(testKey, enoughArray);
asrt (len == RocksDB.NOT_FOUND);
/
/ repeat the test with WriteOptions
try (final WriteOptions writeOpts = new WriteOptions()) {
writeOpts.tSync(true);
writeOpts.tDisableWAL(true);
db.put(writeOpts, testKey, testValue);
自编山歌歌词大全
len = db.get(testKey, enoughArray);
asrt (len == testValue.length);
asrt (new String(testValue).equals(
new String(enoughArray, 0, len)));
}
try {
for (final TickerType statsType : TickerType.values()) {
if (statsType != TickerType.TICKER_ENUM_MAX) {
}
}
System.out.println("getTickerCount() pasd.");
} catch (final Exception e) {
System.out.println("Failed in call to getTickerCount()");
推迟的反义词asrt (fal); //Should never reach here.
}
try {
for (final HistogramType histogramType : HistogramType.values()) {  if (histogramType != HistogramType.HISTOGRAM_ENUM_MAX) {  HistogramData data = HistogramData(histogramType);
}
}
System.out.println("getHistogramData() pasd.");
} catch (final Exception e) {
System.out.println("Failed in call to getHistogramData()");
asrt (fal); //Should never reach here.
}
try (final RocksIterator iterator = db.newIterator()) {
boolean ekToFirstPasd = fal;
for (iterator.ekToFirst(); iterator.isValid(); ()) {
iterator.status();
asrt (iterator.key() != null);
asrt (iterator.value() != null);
ekToFirstPasd = true;
}
if (ekToFirstPasd) {
System.out.println("iterator ekToFirst tests pasd.");
}
boolean ekToLastPasd = fal;
for (iterator.ekToLast(); iterator.isValid(); iterator.prev()) {
iterator.status();
asrt (iterator.key() != null);
asrt (iterator.value() != null);
ekToLastPasd = true;
}
if (ekToLastPasd) {
System.out.println("iterator ekToLastPasd tests pasd.");
}
iterator.ekToFirst();
iterator.ek(iterator.key());
asrt (iterator.key() != null);
asrt (iterator.value() != null);
System.out.println("iterator ek test pasd.");
}
System.out.println("iterator tests pasd.");
final List<byte[]> keys = new ArrayList<>();
try (final RocksIterator iterator = db.newIterator()) {
for (iterator.ekToLast(); iterator.isValid(); iterator.prev()) {
keys.add(iterator.key());
}
}
Map<byte[], byte[]> values = db.multiGet(keys);
asrt (values.size() == keys.size());

本文发布于:2023-06-04 10:23:02,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/984443.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据库   例如   环境   作量
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图