<strong><span style= "FONT-SIZE:18PX;" >/*** * @author yangxin * @info the K-means algorithm iterates through the vector elements in multiple sequential order, and the last representation is best for this access pattern. * Using the partial vector of the mapper output, the reducer obtains a complete vector through simple stitching. */package Unittwelve;import Java.io.ioexception;import Org.apache.hadoop.io.text;import Org.apache.hadoop.mapreduce.reducer;import Org.apache.mahout.math.namedvector;import Org.apache.mahout.math.vector;import Org.apache.mahout.math.vectorwritable;public class VectorReducer extends Reducer<text, Vectorwritable, Text, vectorwritable>{private vectorwritable writer = new vectorwritable (); protected void reduce (Text tag, iterable<vectorwritable> values, context context) throws IOException, Interruptedexception{vector vector = null;for (vectorwritable partialvector:values) {if (vector = = null) {vector = Partialvector.get (). like ();} Partialvector.get (). addto (vector);} Namedvector namedvector = new Namedvector (vector, tag.tostring ()); Writer.set (Namedvector); Context.write (tag, writer) ;}} </span></strong>
Combine part of a label vector and accumulate it into a full vector