一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java RocksDB安裝與應(yīng)用

Java RocksDB安裝與應(yīng)用

2021-03-09 13:28yu8huan Java教程

本篇文章主要給大家介紹了JAVA中RocksDB的安裝與應(yīng)用,有需要到的朋友一起學(xué)習(xí)參考下。

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ǔ)的操作,例如打開(kāi)和關(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的依賴,就可以在開(kāi)發(fā)環(huán)境中自我嘗試了。如果你沒(méi)有理解這點(diǎn),你就可能會(huì)走入下面這兩種不推薦的安裝方式。

方式 一   去查看rocksdb 的官網(wǎng) 發(fā)現(xiàn)要寫 一個(gè)c++ 程序(不推薦)

?
1
2
3
4
5
6
7
8
#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 不一樣,為啥沒(méi)有一個(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ì)的安裝文檔見(jiàn)pyrocksdb 的官網(wǎng)安裝文檔。

以上兩種方式對(duì)于熟悉c++ 或者python 的開(kāi)發(fā)者來(lái)說(shuō)都比較友好,但對(duì)于java 開(kāi)發(fā)者來(lái)說(shuō)不是太友好。

接下來(lái)就介紹第三種方式。

方式三 使用maven (推薦)

新建maven 項(xiàng)目,修改pom.xml 依賴?yán)锩嫣砑?/p>

?
1
2
3
4
5
<dependency>
 <groupid>org.rocksdb</groupid>
 <artifactid>rocksdbjni</artifactid>
 <version>5.8.6</version>
</dependency>

可以選擇你喜歡的版本。

然后更高maven 的語(yǔ)言級(jí)別,我這里全局設(shè)置為了1.8

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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è)類。

Java RocksDB安裝與應(yīng)用

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

Java RocksDB安裝與應(yīng)用

Java RocksDB安裝與應(yīng)用

后面會(huì)更新更多關(guān)于rockdb 開(kāi)發(fā)api 的介紹,以及在生產(chǎn)中的應(yīng)用,希望大家關(guān)注。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// 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ì)服務(wù)器之家的支持。

原文鏈接:https://my.oschina.net/yu8huan/blog/1590900

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美在线欧美 | avtt天堂网 手机资源 | 精品精品国产自在久久高清 | 四虎精品成人免费观看 | 无码专区aaaaaa免费视频 | 91嫩草私人成人亚洲影院 | 亚洲精品乱码久久久久久蜜桃图片 | 鬼吹灯天星术在线高清观看 | 日韩精品视频在线观看免费 | 欧美日韩亚洲第一区在线 | 91麻豆精品国产自产在线 | 国产在线播放一区 | 日本偷偷操 | 国产毛片在线观看 | 亚洲sss综合天堂久久久 | t66y地址一地址二地址三 | 841995论坛网站2022年 | 亚欧精品在线观看 | 女上男下gifxxoo动态视频 | 日韩毛片免费在线观看 | 热99精品| 四虎影视入口 | 涩涩漫画软件 | 精品四虎国产在免费观看 | 欧美日本一本线在线观看 | 国产播放器一区 | 日本十大顶级绝伦推理片 | 91色香sxmv最网页版新地址 | 欧美一区二区免费 | 全黄h全肉细节修仙玄幻文 全彩调教侵犯h本子全彩妖气he | 亚洲H成年动漫在线观看不卡 | 91大神在线观看精品一区 | 蜜桃影像传媒推广 | 范冰冰上面好大下面好紧 | 亚洲欧美专区精品久久 | 久9视频这里只有精品123 | 情欲综合网 | 男人猛进猛出女人下面视频 | 色老板影视 | av中文字幕在线 | 天堂俺去俺来也www久久婷婷 |