MapDB提供了Java映射、集、列表、队列和其他由非堆或磁盘存储支持的集合。它是java集合框架和嵌入式数据库引擎之间的混合。它是Apache许可下的免费和开放源码。


如果处理GB级数据,请考虑使用非缓存存储容器,比如本篇提到的MapDB,而不要把庞大的对象直接放入内存或redis缓存数据库中,当然,最快的就是内存操作,如果对象的容量没有超过内存的四分之三,可以考虑使用内存!


用法   --- Spring测试单元中进行简单使用


pom.xml


<!-- https://mvnrepository.com/artifact/org.mapdb/mapdb -->
<dependency>
	<groupId>org.mapdb</groupId>
	<artifactId>mapdb</artifactId>
	<version>3.0.5</version>
</dependency>


最新maven依赖包,请查看地址:http://mvnrepository.com/artifact/org.mapdb/mapdb


测试用例


DBTest.java


package onegis.osmosis;

import java.io.File;

import org.junit.Test;
import org.mapdb.BTreeMap;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;

public class DBTest {

	@Test
	public void Test(){
		
		DB db = DBMaker.fileDB("D:/data/tempdata/myDB")
				.fileMmapEnable() 
				.fileMmapEnableIfSupported() 
				.fileMmapPreclearDisable() 
				.allocateIncrement(512 * 1024 * 1024)
				.cleanerHackEnable()
				.make();


		BTreeMap<Long, int[]> myMap = db.treeMap("data")
				.keySerializer(Serializer.LONG)
				.valueSerializer(Serializer.INT_ARRAY)
				.createOrOpen();
		
		double x = 127.4521364;
		double y = 56.1452147;
		myMap.put(10001L, new int[]{(int)(x*1000000),(int)(y*1000000)});
		int[] point = myMap.get(10001L);
		
		System.err.println("Point("+point[0]/1000000d+","+point[1]/1000000d+")");
		
		//用完请关闭db
		
		db.close();

		// 删除文件
		/*File file = new File("D:/data/tempdata/myDB");
		if (file.exists() && file.isFile()) {
			file.delete();
		}*/
	}
}



运行测试方法,效果如下









Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐