rocksdb 是一個可嵌入的,持久性的 key-value存儲。
以下介紹來自rocksdb 中文官網
https://rocksdb.org.cn/
它有以下四個特點
1 高性能:rocksdb使用一套日志結構的數據庫引擎,為了更好的性能,這套引擎是用c++編寫的。 key和value是任意大小的字節流。
2 為快速存儲而優化:rocksdb為快速而又低延遲的存儲設備(例如閃存或者高速硬盤)而特殊優化處理。 rocksdb將最大限度的發揮閃存和ram的高度率讀寫性能。
3 可適配性 :rocksdb適合于多種不同工作量類型。 從像myrocks這樣的數據存儲引擎, 到應用數據緩存, 甚至是一些嵌入式工作量,rocksdb都可以從容面對這些不同的數據工作量需求。
4 基礎和高級的數據庫操作 rocksdb提供了一些基礎的操作,例如打開和關閉數據庫。 對于合并和壓縮過濾等高級操作,也提供了讀寫支持。
??????rockdb 安裝與使用
rocksdb 安裝有多種方式。由于官方沒有提供對應平臺的二進制庫,所以需要自己編譯使用。
rocksdb 的安裝很簡單,但是需要轉變一下對于rocksdb 的看法。它不是一個重量級別的數據庫,是一個嵌入式的key-value 存儲。這意味著你只要在你的maven項目中添加 rocksdb的依賴,就可以在開發環境中自我嘗試了。如果你沒有理解這點,你就可能會走入下面這兩種不推薦的安裝方式。
方式 一 去查看rocksdb 的官網 發現要寫 一個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()); |
創建一個數據庫???? 怎么和之前用的mysql 或者mongo 不一樣,為啥沒有一個start.sh 或者start.bat 之類的腳本。難道要我寫。寫完了編譯發現還不知道怎么和rocksdb 庫進行關聯,怎么辦,我c++都忘完了。
方式二 使用pyrocksdb (不推薦)
http://pyrocksdb.readthedocs.io/en/latest/installation.html
詳細的安裝文檔見pyrocksdb 的官網安裝文檔。
以上兩種方式對于熟悉c++ 或者python 的開發者來說都比較友好,但對于java 開發者來說不是太友好。
接下來就介紹第三種方式。
方式三 使用maven (推薦)
新建maven 項目,修改pom.xml 依賴里面添加
1
2
3
4
5
|
<dependency> <groupid>org.rocksdb</groupid> <artifactid>rocksdbjni</artifactid> <version> 5.8 . 6 </version> </dependency> |
可以選擇你喜歡的版本。
然后更高maven 的語言級別,我這里全局設置為了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> |
到這里,環境就裝好了,是不是又回到了熟悉的java 世界。
然后copy 源碼包下的一個類,在ide中修改一下運行配置,加一個程序運行中數據庫存儲路徑,就可以運行測試了 。我會在文章最后給出這個類。
運行控制臺會有日志輸出,同時也文件中也會出現一下新的文件。
后面會更新更多關于rockdb 開發api 的介紹,以及在生產中的應用,希望大家關注。
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安裝與應用的全部內容,如果大家在學習后還有任何不明白的可以在下方的留言區域討論,感謝對服務器之家的支持。
原文鏈接:https://my.oschina.net/yu8huan/blog/1590900