最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java RocksDB安裝與應用

 更新時間:2017年12月21日 10:49:45   投稿:laozhang  
本篇文章主要給大家介紹了JAVA中RocksDB的安裝與應用,有需要到的朋友一起學習參考下。

rocksDB 是一個可嵌入的,持久性的 key-value存儲。

以下介紹來自rocksDB 中文官網 

https://rocksdb.org.cn/

它有以下四個特點

1 高性能:RocksDB使用一套日志結構的數據庫引擎,為了更好的性能,這套引擎是用C++編寫的。 Key和value是任意大小的字節(jié)流。

2 為快速存儲而優(yōu)化:RocksDB為快速而又低延遲的存儲設備(例如閃存或者高速硬盤)而特殊優(yōu)化處理。 RocksDB將最大限度的發(fā)揮閃存和RAM的高度率讀寫性能。

3 可適配性 :RocksDB適合于多種不同工作量類型。 從像MyRocks這樣的數據存儲引擎, 到應用數據緩存, 甚至是一些嵌入式工作量,RocksDB都可以從容面對這些不同的數據工作量需求。

4 基礎和高級的數據庫操作  RocksDB提供了一些基礎的操作,例如打開和關閉數據庫。 對于合并和壓縮過濾等高級操作,也提供了讀寫支持。

​​​​​​RockDB 安裝與使用

rocksDB 安裝有多種方式。由于官方沒有提供對應平臺的二進制庫,所以需要自己編譯使用。

rocksDB 的安裝很簡單,但是需要轉變一下對于rocksDB 的看法。它不是一個重量級別的數據庫,是一個嵌入式的key-value 存儲。這意味著你只要在你的Maven項目中添加 rocksDB的依賴,就可以在開發(fā)環(huán)境中自我嘗試了。如果你沒有理解這點,你就可能會走入下面這兩種不推薦的安裝方式。

方式 一   去查看rocksDB 的官網 發(fā)現要寫 一個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)建一個數據庫???? 怎么和之前用的mysql 或者mongo 不一樣,為啥沒有一個start.sh 或者start.bat 之類的腳本。難道要我寫。寫完了編譯發(fā)現還不知道怎么和rocksDB 庫進行關聯,怎么辦,我C++都忘完了。

方式二  使用pyrocksDB (不推薦)

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

詳細的安裝文檔見pyrocksDB 的官網安裝文檔。

以上兩種方式對于熟悉C++ 或者python 的開發(fā)者來說都比較友好,但對于java 開發(fā)者來說不是太友好。

接下來就介紹第三種方式。

方式三 使用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>

到這里,環(huán)境就裝好了,是不是又回到了熟悉的java 世界。

然后copy 源碼包下的一個類,在IDE中修改一下運行配置,加一個程序運行中數據庫存儲路徑,就可以運行測試了 。我會在文章最后給出這個類。

運行控制臺會有日志輸出,同時也文件中也會出現一下新的文件。

后面會更新更多關于rockDB 開發(fā)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安裝與應用的全部內容,如果大家在學習后還有任何不明白的可以在下方的留言區(qū)域討論,感謝對腳本之家的支持。

相關文章

  • 淺析Java 反射機制的用途和缺點

    淺析Java 反射機制的用途和缺點

    這篇文章給大家分析了Java 反射機制的用途和缺點以及相關知識點內容,有興趣的朋友可以參考學習下。
    2018-07-07
  • @scope("prototype") @loadbalanced注解負載均衡失效問題

    @scope("prototype") @loadbalanced注解負載均衡失效問題

    這篇文章主要為大家介紹了@scope("prototype") @loadbalanced注解負載均衡失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • JavaBean四個作用域范圍的詳解

    JavaBean四個作用域范圍的詳解

    這篇文章主要介紹了JavaBean四個作用域范圍的詳解的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • Jenkins安裝多個jdk版本并在項目中選擇對應jdk版本

    Jenkins安裝多個jdk版本并在項目中選擇對應jdk版本

    在使用jenkins構建項目時會遇到不同的job需要配置不同版本的jdk,下面這篇文章主要給大家介紹了關于Jenkins安裝多個jdk版本并在項目中選擇對應jdk版本的相關資料,需要的朋友可以參考下
    2024-03-03
  • 源碼解析帶你了解LinkedHashMap

    源碼解析帶你了解LinkedHashMap

    大多數情況下,只要不涉及線程安全問題,Map基本都可以使用HashMap,不過HashMap有一個問題,就是迭代HashMap的順序并不是HashMap放置的順序,也就是無序。HashMap的這一缺點往往會帶來困擾,所以LinkedHashMap就閃亮登場了,這篇文章通過源碼解析帶你了解LinkedHashMap
    2021-09-09
  • java list去重操作實現方式

    java list去重操作實現方式

    本文主要介紹了java list 去重的方法,其中有帶類型寫法和不帶類型寫法,并舉例測試,具有一定參考借鑒價值,希望能對有需要的小伙伴有所幫助
    2016-07-07
  • SpringBoot接收請求參數的四種方式總結

    SpringBoot接收請求參數的四種方式總結

    這篇文章主要給大家介紹了關于SpringBoot接收請求參數的四種方式,文中通過代碼以及圖文介紹的非常詳細,對大家學習或者使用SpringBoot具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • springboot整合shiro多驗證登錄功能的實現(賬號密碼登錄和使用手機驗證碼登錄)

    springboot整合shiro多驗證登錄功能的實現(賬號密碼登錄和使用手機驗證碼登錄)

    這篇文章給大家介紹springboot整合shiro多驗證登錄功能的實現方法,包括賬號密碼登錄和使用手機驗證碼登錄功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2021-07-07
  • Java 數據結構之時間復雜度與空間復雜度詳解

    Java 數據結構之時間復雜度與空間復雜度詳解

    算法復雜度分為時間復雜度和空間復雜度。其作用: 時間復雜度是度量算法執(zhí)行的時間長短;而空間復雜度是度量算法所需存儲空間的大小
    2021-11-11
  • 基于雪花算法實現增強版ID生成器詳解

    基于雪花算法實現增強版ID生成器詳解

    這篇文章主要為大家詳細介紹了如何基于雪花算法實現增強版ID生成器,文中的示例代碼講解詳細,對我們學習具有一定的借鑒價值,需要的可以了解一下
    2022-10-10

最新評論

阿坝| 隆德县| 潞城市| 梧州市| 库伦旗| 北安市| 波密县| 水城县| 石首市| 定襄县| 咸丰县| 江西省| 和龙市| 平山县| 海阳市| 博白县| 邵武市| 竹山县| 綦江县| 墨玉县| 旬阳县| 嘉义市| 丽水市| 北宁市| 鄂伦春自治旗| 乌兰察布市| 吉木萨尔县| 时尚| 牙克石市| 西林县| 宜章县| 甘泉县| 庆阳市| 三原县| 横山县| 太仆寺旗| 涪陵区| 青阳县| 吴堡县| 赤峰市| 三原县|