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集群跑,才会写入日记文件。这里不是在聚群上跑,所以提示警告。
时间: 2024-10-09 23:46:45