简体   繁体   English

Hadoop Reducer不起作用

[英]Hadoop Reducer does not work

I am having trouble with a MapReduce Job. 我在使用MapReduce作业时遇到麻烦。 My map function does run and it produces the desired output. 我的map函数确实运行,并且会产生所需的输出。 However, the reduce function does not run. 但是, reduce函数不会运行。 It seems like the function never gets called. 似乎该函数永远不会被调用。 I am using Text as keys and Text as values. 我使用文本作为键,使用文本作为值。 But I don't think that this causes the problem. 但是我不认为这会导致问题。

The input file is formatted as follows: 输入文件的格式如下:

2015-06-06,2015-06-06,40.80239868164062,-73.93379211425781,40.72591781616211,-73.98358154296875,7.71,35.72
2015-06-06,2015-06-06,40.71020126342773,-73.96302032470703,40.72967529296875,-74.00226593017578,3.11,2.19
2015-06-05,2015-06-05,40.68404388427734,-73.97597503662109,40.67932510375977,-73.95581817626953,1.13,1.29
...

I want to extract the second date of a line as Text and use it as key for the reduce. 我想将一行的第二个日期提取为Text并将其用作减少的键。 The value for the key will be a combination of the last two float values in the same line. 键的值将是同一行中最后两个float值的组合。
ie: 2015-06-06 7.71 35.72 2015-06-06 9.71 66.72 即: 2015-06-06 7.71 35.72 2015-06-06 9.71 66.72
So that the value part can be viewed as two columns separated by a blank. 这样就可以将值部分视为由空格分隔的两列。
That actually works and I get an output file with many same keys but different values. 实际可行,我得到了一个输出文件,其中包含许多相同的键但值不同。

Now I want to sum up the both of the float columns for each key, so that after the reduce I get a date as key with the summed up columns as value. 现在,我想对每个键的两个浮点列进行求和,以便在reduce之后,我得到一个日期作为键,将求和列作为值。

Problem: reduce does not run. 问题:reduce无法运行。

See the code below: 请参见下面的代码:

Mapper 映射器

public class Aggregate {

public static class EarnDistMapper extends Mapper<Object, Text, Text, Text> {

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

        String [] splitResult = value.toString().split(",");
        String dropOffDate = "";
        String compEarningDist = "";
        //dropoffDate at pos 1 as key 
        dropOffDate = splitResult[1];
        //distance at pos length-2 and earnings at pos length-1 as values separated by space
        compEarningDist = splitResult[splitResult.length -2] + " " + splitResult[splitResult.length-1];

        context.write(new Text(dropOffDate), new Text(compEarningDist));
    }
}

Reducer 减速器

public static class EarnDistReducer extends Reducer<Text,Text,Text,Text> {

    public void reduce(Text key, Iterator<Text> values, Context context) throws IOException, InterruptedException {

         float sumDistance = 0;
         float sumEarnings = 0;
         String[] splitArray; 

         while (values.hasNext()){
             splitArray = values.next().toString().split("\\s+");
             //distance first
             sumDistance += Float.parseFloat(splitArray[0]);
             sumEarnings += Float.parseFloat(splitArray[1]);
         }

         //combine result to text

         context.write(key, new Text(Float.toString(sumDistance) + " " + Float.toString(sumEarnings)));
    }
}

Job 工作

public static void main(String[] args) throws Exception{
    // TODO
    Configuration conf = new Configuration();

    Job job = Job.getInstance(conf, "Taxi dropoff");
    job.setJarByClass(Aggregate.class);
    job.setMapperClass(EarnDistMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Text.class);
    job.setCombinerClass(EarnDistReducer.class);
    job.setReducerClass(EarnDistReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

Thank you for your help!! 谢谢您的帮助!!

You have the signature of the reduce method wrong. 您具有reduce方法的签名错误。 You have: 你有:

public void reduce(Text key, Iterator<Text> values, Context context) {

It should be: 它应该是:

public void reduce(Text key, Iterable<Text> values, Context context) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM