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è)類。
運(yùn)行控制臺(tái)會(huì)有日志輸出,同時(shí)也文件中也會(huì)出現(xiàn)一下新的文件。
后面會(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