电影数据集的处理
? ? ?最近將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("\t");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 blocke.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);個人感覺這樣處理有些麻煩,無奈編程技術有限,只想到了這樣一個爛辦法,往后還需要加強學習!
?
轉載于:https://www.cnblogs.com/bowenlearning/p/3762009.html
總結
- 上一篇: [实验]通过内核Patch去掉iOS-v
- 下一篇: mongodb first