Mac环境下RocksDB Java版例子

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();
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值