Convert to MapReduce: Calculate matrix multiplication by partial score

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.