LevelDB 编译安装(Debian 8)

环境信息

虚拟机节点(192.168.100.171<debian171>)

Debian jessie 8.5 

LevelDB 1.18

安装leveldb

sudo apt-get install libsnappy-dev
sudo tar xvf leveldb-1.18.tar.gz
cd leveldb-1.18/
sudo make
sudo cp libleveldb.so.1.18 /usr/local/lib
sudo cp libleveldb.a /usr/local/lib
sudo cp -R include/leveldb /usr/local/include
cd /usr/local/lib
sudo ln -s libleveldb.so.1.18 libleveldb.so
sudo ln -s libleveldb.so.1.18 libleveldb.so.1
sudo ldconfig

java代码

package surfin.example.leveldb.simpledemo;

import static org.iq80.leveldb.impl.Iq80DBFactory.asString;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.iq80.leveldb.impl.Iq80DBFactory.factory;

import java.io.File;
import java.io.IOException;

import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBComparator;
import org.iq80.leveldb.DBIterator;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.ReadOptions;
import org.iq80.leveldb.WriteBatch;
import org.iq80.leveldb.WriteOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleDemoBootstrap {
	private static final Logger log = LoggerFactory.getLogger(SimpleDemoBootstrap.class);

	public static void main(String[] args) {

		Options options = new Options();
		options.createIfMissing(true);
		DB db = null;
		try {
			db = factory.open(new File("D:/leveldb-example/simpledemo"), options);
			
			//put get delete
			db.put(bytes("Tampa"), bytes("rocks"));
			byte[] tampa_b = db.get(bytes("Tampa"));
			log.info("Tampa: {}", asString(tampa_b));
			WriteOptions wo = new WriteOptions().sync(true);
			db.delete(bytes("Tampa"), wo);
			tampa_b = db.get(bytes("Tampa"));
			log.info("Tampa: {}", asString(tampa_b));

			//write batch
			WriteBatch wb = db.createWriteBatch();
			try{
				
				wb.put(bytes("Tampa"), bytes("green"));
				wb.put(bytes("London"), bytes("red"));
				db.write(wb);
			}finally{
				wb.close();
			}
			
			//Iterating key/values.
			DBIterator iter = db.iterator();
			try{
				for(iter.seekToFirst();iter.hasNext();iter.next()){
					String key = asString(iter.peekNext().getKey());
					String val = asString(iter.peekNext().getValue());
					log.info("iter {}:{}", key, val);
				}
				
			}finally{
				iter.close();
			}
			
			
			//Working against a Snapshot view of the Database.
			ReadOptions ro = new ReadOptions();
			ro.snapshot(db.getSnapshot());
			try {
				iter = db.iterator(ro);
				try{
					for(iter.seekToFirst();iter.hasNext();iter.next()){
						String key = asString(iter.peekNext().getKey());
						String val = asString(iter.peekNext().getValue());
						log.info("snapshot iter {}:{}", key, val);
					}
					
					log.info("snapshot Tampa: {}",asString(db.get(bytes("Tampa"), ro)));
					
				}finally{
					iter.close();
				}

			} finally {
			  // Make sure you close the snapshot to avoid resource leaks.
			  ro.snapshot().close();
			}
			
			//OUsing a custom Comparator.

			DBComparator comparator = new DBComparator(){
			    public int compare(byte[] key1, byte[] key2) {
			        return new String(key1).compareTo(new String(key2));
			    }
			    public String name() {
			        return "simple";
			    }
			    public byte[] findShortestSeparator(byte[] start, byte[] limit) {
			        return start;
			    }
			    public byte[] findShortSuccessor(byte[] key) {
			        return key;
			    }
			};
			
//			log.info(String.valueOf(System.currentTimeMillis()));
			

		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}finally{
			if(db != null){
				try {
					db.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}
}

pom依赖

<dependency>
	<groupId>org.iq80.leveldb</groupId>
	<artifactId>leveldb</artifactId>
</dependency>

参考资料

https://gist.github.com/dustismo/6203329

https://techoverflow.net/blog/2012/12/14/compiling-installing-leveldb-on-linux/

转载于:https://my.oschina.net/norxiva/blog/711063

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值