@Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line, " "); int dim = tokenizer.countTokens(); double[] coords = new double[dim]; for (int i = 0; i < dim; i++) { coords[i] = Double.valueOf(tokenizer.nextToken()); } Point point = new Point(coords); Centroid nearest = null; double nearestDistance = Double.MAX_VALUE; for (Centroid c : centers) { double dist = point.euclideanDistance(c); if (nearest == null) { nearest = c; nearestDistance = dist; } else { if (dist < nearestDistance) { nearest = c; nearestDistance = dist; } } } context.write(nearest, point); }
@Override protected void map(Centroid key, Point value, Context context) throws IOException, InterruptedException { Centroid nearest = null; double nearestDistance = Double.MAX_VALUE; for (Centroid c : centers) { double dist = value.euclideanDistance(c); if (nearest == null) { nearest = c; nearestDistance = dist; } else { if (dist < nearestDistance) { nearest = c; nearestDistance = dist; } } } context.write(nearest, value); }