• 电影数据集的处理


         最近将movielens数据集rate.dat原来格式:userid,itemid,rating,timestamp按要求转换为(userid,item1,item2....),按时间的前后展示用户的所以观看电影记录。我是首先将数据导入到mysql数据库,利用数据库的容易对数据排序的特点,将其按时间字段进行排序,将排序的查询结果导出到本地文件,

     select userID,movieID,timestamp into outfile1 '/var/lib/mysql/outfile.txt' from movie_jieguo2  order by userID,timestamp;

    1 168 874965478
    1 172 874965478
    1 165 874965518
    1 156 874965556
    1 166 874965677

    接下来,考虑将每一个用户的观看电影记录连接起来,运用java的hashmap进行处理

    public static void main(String[] args) {
            String line=null;
            StringBuilder sb=new StringBuilder();
            Map<String,String> movie_time = new HashMap<String, String>();
        try {
            
            LineNumberReader lineReader = new LineNumberReader(new FileReader("/home/grid/outfile1.txt"));
            
            while ((line = lineReader.readLine()) != null) {
                String[] ra = line.split("	");
                String userID=ra[0];
                String itemID=ra[1];
                
                if(movie_time.containsKey(userID)){
                    sb.append(itemID+" ");
                    movie_time.put(userID,sb.toString());
                }
                else{
                    sb.delete( 0, sb.length() );
                    sb.append(itemID+" ");
                    movie_time.put(userID,sb.toString());
                    
                }
            
            }
            
        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();}

    学到了StringBuilder清空的方法:sb.delete( 0, sb.length() );网上的一些帖子认为该清空方法最有效,开始一直傻B地用null来清空,结果一直报空指针错误,弄了很久,没找到原因。

    最后用遍历hashmap将输出重定向到本地文本文件中:

    遍历hashmap网上帖子推荐方法:

    Iterator iter = movie_time.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            System.out.println(key+"    "+val);

    个人感觉这样处理有些麻烦,无奈编程技术有限,只想到了这样一个烂办法,往后还需要加强学习!

  • 相关阅读:
    java环境配置为1.7jdk为什么cmd java -version查看版本是1.8
    bulid path 引 jar包 步骤
    eclipse 报错
    PLSQL使用技巧
    Oracle sqlplus不是内部或外部命令
    SVN 插件安装到Myeclipse10 上(经典)
    socket学习
    Eclipse 配置 插件svn 包步骤
    如何在Eclipse中使用SVN(经典)
    linux 下搭建LAMP
  • 原文地址:https://www.cnblogs.com/bowenlearning/p/3762009.html
Copyright © 2020-2023  润新知