1.在elcipse上建立一個java project 項目名:mymahout
2.建立libs文件夾,在mahout 0.9的lib文件夾下找到一下java包
其中log4j.properties可在hadoop文件夾下找到。
把他們放到libs文件夾下。
3.把文件夾libs復制到mymahout項目下
4.點擊libs文件夾,右鍵,選擇Build Path,把libs文件夾下的文件引入。
5.建立類mahout,寫入如下代碼

import java.io.File; import java.util.List; import org.apache.mahout.cf.taste.impl.model.file.*; import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood; import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; import org.apache.mahout.cf.taste.model.DataModel; import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood; import org.apache.mahout.cf.taste.recommender.RecommendedItem; import org.apache.mahout.cf.taste.recommender.Recommender; import org.apache.mahout.cf.taste.similarity.UserSimilarity; public class Mahout { public static void main(String[] args) throws Exception { DataModel model=new FileDataModel(new File("/Users/jia/Desktop/test.txt")); UserSimilarity similarity =new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood =new NearestNUserNeighborhood(2,similarity,model); Recommender recommender= new GenericUserBasedRecommender(model,neighborhood,similarity); List<RecommendedItem> recommendations =recommender.recommend(1, 2); for(RecommendedItem recommendation :recommendations){ System.out.println(recommendation); } } }
6.運行結果
紅色部分提示,如果結合hadoop集群跑,才會寫入日記文件。這里不是在聚群上跑,所以提示警告。