【发布时间】:2015-08-27 04:27:45
【问题描述】:
我有一个 MapReduce 作业,它输出一个 IntWritable 作为键和 Point(我创建的实现可写的对象)对象作为 map 函数的值。然后在 reduce 函数中,我使用 for-each 循环遍历 Points 的可迭代对象来创建列表:
@Override
public void reduce(IntWritable key, Iterable<Point> points, Context context) throws IOException, InterruptedException {
List<Point> pointList = new ArrayList<>();
for (Point point : points) {
pointList.add(point);
}
context.write(key, pointList);
}
问题是这个列表的大小是正确的,但每个点都是完全相同的。我的 Point 类中的字段不是静态的,我在循环中单独打印了每个点,以确保这些点是唯一的(它们是唯一的)。此外,我创建了一个单独的类,它只创建几个点并将它们添加到列表中,这似乎有效,这意味着 MapReduce 做了一些我不知道的事情。
任何解决此问题的帮助将不胜感激。
更新: Mapper 类代码:
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
private IntWritable firstChar = new IntWritable();
private Point point = new Point();
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line, " ");
while(tokenizer.hasMoreTokens()) {
String atts = tokenizer.nextToken();
String cut = atts.substring(1, atts.length() - 1);
String[] nums = cut.split(",");
point.set(Double.parseDouble(nums[0]), Double.parseDouble(nums[1]), Double.parseDouble(nums[2]), Double.parseDouble(nums[3]));
context.write(one, point);
}
}
点类:
public class Point implements Writable {
public Double att1;
public Double att2;
public Double att3;
public Double att4;
public Point() {
}
public void set(Double att1, Double att2, Double att3, Double att4) {
this.att1 = att1;
this.att2 = att2;
this.att3 = att3;
this.att4 = att4;
}
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeDouble(att1);
dataOutput.writeDouble(att2);
dataOutput.writeDouble(att3);
dataOutput.writeDouble(att4);
}
@Override
public void readFields(DataInput dataInput) throws IOException {
this.att1 = dataInput.readDouble();
this.att2 = dataInput.readDouble();
this.att3 = dataInput.readDouble();
this.att4 = dataInput.readDouble();
}
@Override
public String toString() {
String output = "{" + att1 + ", " + att2 + ", " + att3 + ", " + att4 + "}";
return output;
}
【问题讨论】:
-
请按照您在map中设置和在reduce中检索的方式添加map和reduce的代码。也是实现Writable的点类
-
刚刚用 Point 和 Mapper 类更新了帖子。上面的所有代码都是每个类中的所有内容。
-
尝试移动Point point = new Point();在地图内部,并采用 context.write(one, point);在while循环之外。
标签: java list hadoop mapreduce reduce