Hadoop 实现协同过滤 (example in <Mahout in action> chapter 6) Part 1
最近一直在研究《Mahout in Action》,今天才算是把第一部分看完。在Chapter 6中有一个例子,是实现协同过滤进行推荐的例子,不过书上的是针对布尔值的输入数据,在mahout的安装目录里面也有这个算法的详细源码,但是毕竟是源码,读起来有点晦涩,所以就参考了书上的例子编写了(书上的例子思路比较清楚)不仅仅是布尔值的输入数据的代码;
下面就详细说下思路及代码:
输入数据:
package org.fansy.date1012.mahoutinaction.chapter6.sourcecode;import java.io.IOException;import java.util.Iterator;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.mapreduce.Mapper;import org.apache.mahout.cf.taste.hadoop.item.VectorOrPrefWritable;import org.apache.mahout.math.VarLongWritable;import org.apache.mahout.math.Vector;import org.apache.mahout.math.VectorWritable;public class WikiMapper32 extends Mapper<VarLongWritable ,VectorWritable,IntWritable,VectorOrPrefWritable>{public void map(VarLongWritable key,VectorWritable value,Context context) throws IOException, InterruptedException{long userID=key.get();Vector userVector=value.get();Iterator<Vector.Element> it=userVector.iterateNonZero();IntWritable itemi=new IntWritable();while(it.hasNext()){Vector.Element e=it.next();int itemIndex=e.index();float preferenceValue=(float)e.get();itemi.set(itemIndex);context.write(itemi, new VectorOrPrefWritable(userID,preferenceValue));//System.out.println("item :"+itemi+",userand val:"+userID+","+preferenceValue);}//System.out.println();}}
下接 Hadoop 实现协同过滤 (example in <Mahout in action> chapter 6) Part 2
分享,快乐,成长