The code consists of four parts:
<strong><span style= "FONT-SIZE:18PX;" >/*** * @author Yangxin * @info Package co-existing series */package Unitsix;import Java.io.ioexception;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.vectorwritable;public Class Cooccurrencecolumnwrappermapper extends Mapper<intwritable, vectorwritable, Intwritable, vectororprefwritable>{public void Map (intwritable key, vectorwritable value, Context context) throws IOException, Interruptedexception{context.write (Key, New Vectororprefwritable (Value.get ()));}} </span></strong>
<strong><span style= "FONT-SIZE:18PX;" >/*** * @author yangxin * @info split number of users */package unitsix;import java.io.ioexception;import java.util.iterator;import o Rg.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 Uservectorsplittermapper 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.nonzeroes (). Iterator (); intwritable itemindexwritable = new Intwritable (), while (It.hasnext ()) {vector.element E = It.next (); int itemIndex = E.index (); float Preferencevalue = (float) e.get (); Itemindexwritable.set (Itemindex); Context.write (itemindexwritable, New vectororprefwritable (UserID, Preferencevalue));}}} </span></strong>
<strong><span style= "FONT-SIZE:18PX;" >/*** * @author yangxin * @info calculate part of the recommended vector */package unitsix;import java.io.ioexception;import java.util.list;import org . Apache.hadoop.io.intwritable;import Org.apache.hadoop.mapreduce.mapper;import Org.apache.mahout.cf.taste.hadoop.item.vectorandprefswritable;import org.apache.mahout.math.VarLongWritable; Import Org.apache.mahout.math.vector;import Org.apache.mahout.math.vectorwritable;public class Partialmultiplymapper extends Mapper<intwritable, vectorandprefswritable, varlongwritable, VectorWritable>{ public void Map (intwritable key, vectorandprefswritable vectorandprefswritable, Context context) throws IOException, Interruptedexception{vector cooccurrencecolumn = Vectorandprefswritable.getvector (); list<long> userids = Vectorandprefswritable.getuserids (); list<float> prefvalues = Vectorandprefswritable.getvalues (); for (int i = 0; i < userids.size (); i++) {Long UserID = Userids.get (i); float prefvalue = Prefvalues.get (i);Vector partialproduct = Cooccurrencecolumn.times (Prefvalue); Context.write (new Varlongwritable (UserID), new Vectorwritable (Partialproduct));}}} </span></strong>
<strong><span style= "FONT-SIZE:18PX;" >/*** * @author yangxin * @info Achieve partial results combiner */package unitsix;import java.io.ioexception;import org.apache.hadoop . MapReduce. Reducer;import Org.apache.mahout.math.varlongwritable;import Org.apache.mahout.math.vector;import Org.apache.mahout.math.vectorwritable;public class Aggregatecombiner extends Reducer<varlongwritable, Vectorwritable, varlongwritable, vectorwritable>{public void reduce (varlongwritable key, iterable< Vectorwritable> values, Context context) throws IOException, interruptedexception{vector partial = Null;for ( Vectorwritable vectorwritable:values) {partial = partial = = null? Vectorwritable.get (): Partial.plus (vectorwritable.ge T ());} Context.write (Key, new vectorwritable (partial));}} </span></strong>
Convert to MapReduce: Calculate matrix multiplication by partial score