RocksDB基于Google的LevelDB,但提高了扩展性可以运行在多核处理器上,可以有效使用快速存储,支持IO绑定、内存和一次写负荷。
其架构如下:
1. 编译RocksJava
首先,需要安装好Java环境。。
其次,去Github下载rocksdb源代码。地址:
- https://github.com/facebook/rocksdb.git
然后,用以下命令编译RocksDBJava:
- make rocksdbjava
编译完成后,会在rocksdb源代码目录下的java/target/目录下生成2个jar包:rocksdbjni-4.9.0-osx.jar 和 librocksdbjni-osx.jnilib
所有API文档都生成在:target/apidocs/目录下
2. 搭建开发环境
将rocksdbjni-4.9.0-osx.jar放在工程classpath中,librocksdbjni-osx.jnilib需要和rocksdbjni-4.9.0-osx.jar放在同一目录中。
3. 代码示例
- package com.test;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.rocksdb.ColumnFamilyDescriptor;
- import org.rocksdb.ColumnFamilyHandle;
- import org.rocksdb.ColumnFamilyOptions;
- import org.rocksdb.DBOptions;
- import org.rocksdb.Options;
- import org.rocksdb.RocksDB;
- import org.rocksdb.RocksDBException;
- import org.rocksdb.RocksIterator;
- public class RocksJavaTest {
- private static final String dbPath = "/Users/xiaolongli/Documents/workspace/rocksdb/rocksdb/java/data/";
- static {
- RocksDB.loadLibrary();
- }
- RocksDB rocksDB;
- public RocksJavaTest() throws RocksDBException {
- }
- // RocksDB.DEFAULT_COLUMN_FAMILY
- public void testDefaultColumnFamily() throws RocksDBException {
- Options options = new Options();
- options.setCreateIfMissing(true);
- rocksDB = RocksDB.open(options, dbPath);
- byte[] key = "Hello".getBytes();
- byte[] value = "World".getBytes();
- rocksDB.put(key, value);
- List<byte[]> cfs = RocksDB.listColumnFamilies(options, dbPath);
- for(byte[] cf : cfs) {
- System.out.println(new String(cf));
- }
- byte[] getValue = rocksDB.get(key);
- System.out.println(new String(getValue));
- rocksDB.put("SecondKey".getBytes(), "SecondValue".getBytes());
- List<byte[]> keys = new ArrayList<>();
- keys.add(key);
- keys.add("SecondKey".getBytes());
- Map<byte[], byte[]> valueMap = rocksDB.multiGet(keys);
- for(Map.Entry<byte[], byte[]> entry : valueMap.entrySet()) {
- System.out.println(new String(entry.getKey()) + ":" + new String(entry.getValue()));
- }
- RocksIterator iter = rocksDB.newIterator();
- for(iter.seekToFirst(); iter.isValid(); iter.next()) {
- System.out.println("iter key:" + new String(iter.key()) + ", iter value:" + new String(iter.value()));
- }
- rocksDB.remove(key);
- System.out.println("after remove key:" + new String(key));
- iter = rocksDB.newIterator();
- for(iter.seekToFirst(); iter.isValid(); iter.next()) {
- System.out.println("iter key:" + new String(iter.key()) + ", iter value:" + new String(iter.value()));
- }
- }
- public void testCertainColumnFamily() throws RocksDBException {
- String table = "CertainColumnFamilyTest";
- String key = "certainKey";
- String value = "certainValue";
- List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();
- Options options = new Options();
- options.setCreateIfMissing(true);
- List<byte[]> cfs = RocksDB.listColumnFamilies(options, dbPath);
- if(cfs.size() > 0) {
- for(byte[] cf : cfs) {
- columnFamilyDescriptors.add(new ColumnFamilyDescriptor(cf, new ColumnFamilyOptions()));
- }
- } else {
- columnFamilyDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions()));
- }
- List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
- DBOptions dbOptions = new DBOptions();
- dbOptions.setCreateIfMissing(true);
- rocksDB = RocksDB.open(dbOptions, dbPath, columnFamilyDescriptors, columnFamilyHandles);
- for(int i = 0; i < columnFamilyDescriptors.size(); i++) {
- if(new String(columnFamilyDescriptors.get(i).columnFamilyName()).equals(table)) {
- rocksDB.dropColumnFamily(columnFamilyHandles.get(i));
- }
- }
- ColumnFamilyHandle columnFamilyHandle = rocksDB.createColumnFamily(new ColumnFamilyDescriptor(table.getBytes(), new ColumnFamilyOptions()));
- rocksDB.put(columnFamilyHandle, key.getBytes(), value.getBytes());
- byte[] getValue = rocksDB.get(columnFamilyHandle, key.getBytes());
- System.out.println("get Value : " + new String(getValue));
- rocksDB.put(columnFamilyHandle, "SecondKey".getBytes(), "SecondValue".getBytes());
- List<byte[]> keys = new ArrayList<byte[]>();
- keys.add(key.getBytes());
- keys.add("SecondKey".getBytes());
- List<ColumnFamilyHandle> handleList = new ArrayList<>();
- handleList.add(columnFamilyHandle);
- handleList.add(columnFamilyHandle);
- Map<byte[], byte[]> multiGet = rocksDB.multiGet(handleList, keys);
- for(Map.Entry<byte[], byte[]> entry : multiGet.entrySet()) {
- System.out.println(new String(entry.getKey()) + "--" + new String(entry.getValue()));
- }
- rocksDB.remove(columnFamilyHandle, key.getBytes());
- RocksIterator iter = rocksDB.newIterator(columnFamilyHandle);
- for(iter.seekToFirst(); iter.isValid(); iter.next()) {
- System.out.println(new String(iter.key()) + ":" + new String(iter.value()));
- }
- }
- public static void main(String[] args) throws RocksDBException {
- RocksJavaTest test = new RocksJavaTest();
- // test.testDefaultColumnFamily();
- test.testCertainColumnFamily();
- }
- }