POM依赖
<dependency>
<groupId>org.iq80.leveldb</groupId>
<artifactId>leveldb-api</artifactId>
<version>0.12</version>
</dependency>
<dependency>
<groupId>org.iq80.leveldb</groupId>
<artifactId>leveldb</artifactId>
<version>0.12</version>
</dependency>
工具类
java
package com.sec.iot.util;
import org.apache.commons.lang3.StringUtils;
import org.iq80.leveldb.*;
import org.iq80.leveldb.impl.Iq80DBFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* levedb 工具类
*
*/
public class LevelDBUtil {
//单例
public static final LevelDBUtil lu = new LevelDBUtil();
// private final DBFactory factory = Iq80DBFactory.factory;
private LevelDBUtil() {
}
//初始化连接
private static DB db = null;
public static void init(String path){
//leveldb只需要建立一次连接
LevelDBUtil ldu = LevelDBUtil.lu;
//"/scada/share/odbcleveldb"
ldu.setPath(path);
ldu.getConnect();
}
private boolean isConnect = false;
//存储路径
private String path;
public void getConnect() {
DBFactory factory = new Iq80DBFactory();
//JniDBFactory.factory;
// 默认如果没有则创建
Options options = new Options();
File file = new File(path);
try {
if (!isConnect) {//单列一个对象,而且只建立一次连接
db = factory.open(file, options);
isConnect = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* public static boolean deleteAll(String prefix) {
//String prefix = "UNIT";
List<String> resultList = new ArrayList<>();
DBIterator iterator = db.iterator();
for (iterator.seek(prefix.getBytes()); iterator.hasNext(); iterator.next()) {
String k = JniDBFactory.asString(iterator.peekNext().getKey());
if (!k.startsWith(prefix)) {
break;
}
delete(k);
}
try {
iterator.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}*/
/**
* 拼接字符串
*
* @param arr
* @return
*/
public static String combine(Object... arr) {
StringBuffer buffer = new StringBuffer();
for (Object obj : arr) {
buffer.append(obj);
}
return buffer.toString();
}
public synchronized static boolean put(String key, String data) {
try {
// db.put(JniDBFactory.bytes(key), JniDBFactory.bytes(data), new WriteOptions().sync(true));
db.put(Iq80DBFactory.bytes(key),Iq80DBFactory.bytes(data),new WriteOptions().sync(true));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public synchronized static String get(String key) {
if (StringUtils.isBlank(key)) {
return null;
}
try {
// byte[] r = db.get(JniDBFactory.bytes(key), new ReadOptions().fillCache(true).verifyChecksums(true));
//
// return JniDBFactory.asString(r);
byte[] r = db.get(Iq80DBFactory.bytes(key),new ReadOptions().fillCache(true).verifyChecksums(true));
return Iq80DBFactory.asString(r);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void close() {
try {
db.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 压缩数据
*/
public synchronized static void compactData() {
try {
//全部压缩数据 已经删除的数据,从硬盘上清除;
db.compactRange(null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static boolean delete(String key) {
if (StringUtils.isBlank(key)) {
return false;
}
try {
db.delete(Iq80DBFactory.bytes(key));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public void destroy() {
close();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}