MR迭代的迭代器,无法解析输出值

时间:2017-03-10 14:43:12

标签: mapreduce iterator

mapreduce的原始文件是这样的(分隔符:Tab)

  

Apple 11 12 13

     

Orange 15 26 10

当我尝试使用","来实现添加新功能并分隔数字的方法时,我的预期输出应该是这样的:

  

Apple 3.0:11,12,13

     

Orange 3.0:15,26,10

但最终的输出结果如下:

  

Apple 3.0:11 12 13

     

Orange 3.0:15 26 10

我尝试打印结果进行跟踪,但似乎next()将跳过解析并直接跳出循环。任何人都可以帮忙吗?

public static class Mapper1 extends MapReduceBase 
    implements Mapper<Text, Text, Text, Text> {
    @Override
  public void map(Text key, Text value, OutputCollector<Text, Text> output,
    Reporter reporter) throws IOException {
        output.collect(key, value); 
    }       
}

public static class Reducer1 extends MapReduceBase 
    implements Reducer<Text, Text, Text, Text> {
  public void reduce(Text key, Iterator<Text> values,
        OutputCollector<Text, Text> output, Reporter reporter)
            throws IOException {
        String feature = "3.0:"; 
        boolean first = true;
        while(values.hasNext()) {
            if(!first) {
                feature += ",";
            }           
            feature += values.next().toString() ;
    System.out.println("count"+feature.length+","+feature);  
            first = false;
        }
        output.collect(key, new Text(feature));
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这是因为您的映射器只为每条记录发出一个键值对,这是不期望的。您可以通过在驱动程序代码中将减速器编号设置为0来检查映射器的输出:

job.setNumReduceTasks(0);

Mapper输入:

  

Apple 11 12 13

     

Orange 15 26 10

实际映射器输出:(键,值)

  

(Apple,11 12 13)

     

(Orange,15 26 10)

预期的映射器输出:(键,值)

  

(Apple,11)

     

(Apple,12)

     

...

     

(橙色,10)

您可以修改映射器以为每条记录发出多个键值对,或者使用String类的split()方法从原始字符串中获取子字符串。