-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper.java
More file actions
39 lines (31 loc) · 1.41 KB
/
Copy pathmapper.java
File metadata and controls
39 lines (31 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class KMeansMapper extends Mapper<LongWritable, Text, IntWritable, PointWritable> {
private List<Point> means;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
// Initialize the means from the distributed cache or any other source
// Here, we assume the means are already set and available in the `means` list
means = new ArrayList<>(); // Initialize and populate the means list
}
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// Convert the input value to a Point object
Point point = Point.fromText(value.toString());
// Find the closest mean for the point
int closestMeanIndex = 0;
double closestMeanDistance = Double.MAX_VALUE;
for (int i = 0; i < means.size(); i++) {
double distance = point.calculateDistance(means.get(i));
if (distance < closestMeanDistance) {
closestMeanDistance = distance;
closestMeanIndex = i;
}
}
// Emit the closest mean index as the key and the point as the value
context.write(new IntWritable(closestMeanIndex), new PointWritable(point));
}
}