Skip to content

Instantly share code, notes, and snippets.

@Denis1990
Created September 17, 2016 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Denis1990/fccb55103b981224adb06a7e4d6995df to your computer and use it in GitHub Desktop.
Save Denis1990/fccb55103b981224adb06a7e4d6995df to your computer and use it in GitHub Desktop.
Mapper class
public class KMeansMapper extends Mapper<LongWritable, Text, PointVector, PointVector> {
private int clusters;
private List<ImmutableTriple<Integer, String, PointVector>> centers;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
System.out.println("Inside setup");
this.clusters = Integer.valueOf(context.getConfiguration().get("clusters"));
this.centers = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("/home/denis/centers"));
for(int i = 0; i < clusters; i++) {
centers.add(DocumentRecordParser.parse(br.readLine()));
}
br.close();
}
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
PointVector line = DocumentRecordParser.returnPointVector(value.toString());
System.out.println("Inside map!");
double minDist = Double.MAX_VALUE;
double dist;
PointVector index = null;
EuclideanDistance ed = new EuclideanDistance();
for (ImmutableTriple<Integer, String, PointVector> c : centers) {
dist = ed.compute(line.points(), c.right.points());
if (dist < minDist) {
minDist = dist;
index = c.right;
}
}
context.write(index, line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment