MapDB的使用实战

来源:互联网 发布:mysql u p h端口 编辑:程序博客网 时间:2024/05/23 15:32


MapDB特性

MapDB是一个内嵌的纯Java的数据库,提供了并发的HashMap、TreeMap、Queue,可以基于堆外或者磁盘来存储数据。用户可以通过配置选择不同的机制来提高性能,比如可以配置多种不同的cache来减少反序列化的开销,提高读取性能;可以开启异步写引擎,使用后台线程来进行序列化和存储更新,来提高插入性能,减少rt。它支持ACID事务、MVCC隔离。它的代码精简,只有一个jar包,无其他依赖,总共才200kb。并且高度模块化,用户可以很容易的扩展,添加新特性。 
以下地址为MapDB包的地址:http://download.csdn.net/detail/qy20115549/9686069

数据格式

首先,我来介绍一下数据格式。存在多个文本,每个文本,里面包含电影的id,用户的id及用户对电影的评分,及评分时间。如下面两个表:

这张图为目录下的文本。一个文本表示,一部电影所有用户的评分及评分时间。 
这里写图片描述

这里写图片描述

目标数据格式

以下程序,将所有文件以用户id:电影id作为key值,用户对电影的评分作为value值,存到MapDB中,并从MapDB中读取数据。以下是读取结果。

这里写图片描述

程序

package mapdb;import java.io.File;import java.util.Map;import org.mapdb.DB;import org.mapdb.DBMaker; /**MapDB使用实战 * @author 合肥工业大学管理学院  钱洋 * @blog http://blog.csdn.net/qy20115549/ * 这个类原文地址:http://junsky.iteye.com/blog/1942285 * @mail 1563178220@qq.com */public class StatMapDB {    private static final String MAP_NAME = "STAT_MAP";      private String filePath;      DB db = null;     //需要的数据格式    Map<String,String> statMap = null;      DBMod type = null;      static enum DBMod      {          READ,          WRITE      }      //类的构造方法    public StatMapDB(String filePath,DBMod type)      {          //初始化        this.filePath = filePath;          this.type = type;          init();      }      //初始化mapdb    private void init()      {           File file = new File(filePath);           db = DBMaker                 .newFileDB(file)                 .transactionDisable()                 .asyncWriteFlushDelay(100)                 .make();           if(type.equals(DBMod.WRITE))           {               if(file.exists())               {                   file.delete();                   new File(filePath + ".p").delete();               }               statMap = db.createTreeMap(MAP_NAME).make();           }           else{               statMap = db.getTreeMap(MAP_NAME);           }      }      public Map<String,String> getStatMapDB()      {          return this.statMap;      }      public void close()      {          if(db!=null){              db.close();              db = null;          }      }  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
package mapdb;import java.io.IOException;import java.util.List;import java.util.Map;import java.util.Map.Entry;import main.FileGetData;/**MapDB使用实战 * @author 合肥工业大学管理学院  钱洋 * @blog http://blog.csdn.net/qy20115549/ * @mail 1563178220@qq.com */public class MapDbTest {      private static final String PATH = "E:\\熊强师兄数据处理\\testdb";      private static FileGetData fileGetData=new FileGetData();    private static void write(List<String> filelist) throws IOException {         //调用写的方法        StatMapDB db = new StatMapDB(PATH, StatMapDB.DBMod.WRITE);          //初始化TreeMap        Map<String,String> map = db.getStatMapDB();          //循环所有的文本        for (int i = 0; i < filelist.size(); i++) {            //获取单个文本并循环            List<String> data=fileGetData.getData(filelist.get(i));            for (int j = 1; j< data.size(); j++) {                String userid=data.get(j).split(",")[0];                String movieid=data.get(0).replaceAll(":", "");                String key=userid+":"+movieid;                String value=data.get(j).split(",")[1];                //文本写入                map.put(key,value);            }        }        db.close();      }      //读取MapDB中的数据,并遍历    private static void read(List<String> filelist) throws IOException      {          StatMapDB db = new StatMapDB(PATH, StatMapDB.DBMod.READ);          Map<String,String> map = db.getStatMapDB();          for (Entry<String, String> entry : map.entrySet()) {              String key = entry.getKey();              String value = entry.getValue();              System.out.println("key:"+key+"\tvalue:"+value);        }          db.close();      }      public static void main(String[] args) throws IOException {         //清理内存        System.gc();        //获取目录下所有文件        List<String> fileList=fileGetData.getFiles("E:\\熊强师兄数据处理\\sample\\");        //将目录下的文件写入MapDB        write(fileList);          System.out.println("写入数据完毕!");        //读取数据        read(fileList);      }  }