Created
September 17, 2016 10:42
-
-
Save Denis1990/fccb55103b981224adb06a7e4d6995df to your computer and use it in GitHub Desktop.
Mapper class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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