<strong><span style="font-size:18px;">/*** * @author YangXin * @info K-Means算法会对向量元素进行多次顺序的遍历,上次表示最适合于这种访问模式。 * 利用Mapper输出的部分向量,Reducer通过简单的拼接得到一个完整的向量。 */ 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>
时间: 2024-10-09 14:34:32