为什么我的减速器无法正常工作?

时间:2019-05-01 20:22:30

标签: java hadoop mapreduce

为什么“ set”只有一个元素,而对于前5行输入,它应该具有4个元素,它们具有相同的URL和四个不同的IP。我也使用“ for-each”代替“ iterator”,但是不起作用。有人可以帮我吗?

映射器

public class WordCount {

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

        private Text IP = new Text();
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] tokens = line.split(",");
            word.set(tokens[2]);
            IP.set(tokens[0]);
            context.write(word, IP);
        }
    }

减速器

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

        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            Set<String> set = new HashSet<String>();
            Iterator<Text> iterator = values.iterator();
            while (iterator.hasNext()) {
                set.add(iterator.next().toString());
            }
            int a = set.size();
            String str = String.format("%d", a);
            context.write(key, new Text(str));
        }
    }

工作

    public static void main(String[] args) throws Exception {
        Job job = new Job();
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.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);       
    }
}

输入

"10.131.0.1","[29/Nov/2017:14:31:33","GET / HTTP/1.1","200"
"10.131.0.2","[29/Nov/2017:14:31:38","GET / HTTP/1.1","200"
"10.131.0.3","[29/Nov/2017:14:31:56","GET / HTTP/1.1","200"
"10.131.0.4","[29/Nov/2017:14:32:02","GET / HTTP/1.1","404"
"10.131.0.5","[29/Nov/2017:16:31:39","GET / HTTP/1.1","200"
"10.131.0.1","[29/Nov/2017:14:05:35","GET /contest.php HTTP/1.1","200"
"10.131.0.2","[29/Nov/2017:14:05:38","GET /contest.php HTTP/1.1","200"
"10.131.0.3","[29/Nov/2017:14:05:50","GET /contest.php HTTP/1.1","404"
"10.131.0.1","[29/Nov/2017:13:51:41","GET /login.php HTTP/1.1","200"
"10.131.0.2","[29/Nov/2017:13:51:49","GET /login.php HTTP/1.1","200"
"10.131.0.1","[29/Nov/2017:13:51:46","GET /contestproblem.php?name=RUET%20OJ%20Server%20Testing%20Contest HTTP/1.1","200"
"10.131.0.8","[29/Nov/2017:13:51:46","GET /contestproblen.php?name=RUET%20OJ%20Server%20Testing%20Contest HTTP/1.1","200"

我的结果是

"GET / HTTP/1.1"    1
"GET /contest.php HTTP/1.1" 1
"GET /contestproblem.php?name=RUET%20OJ%20Server%20Testing%20Contest HTTP/1.1"  1
"GET /contestproblen.php?name=RUET%20OJ%20Server%20Testing%20Contest HTTP/1.1"  1
"GET /login.php HTTP/1.1"   1

1 个答案:

答案 0 :(得分:0)

Reducer工作正常,但是Combiner并未按照您的想法进行。启用合并器的过程是:

地图输出:

("GET / HTTP/1.1", "10.31.0.1")
("GET / HTTP/1.1", "10.31.0.2")

COMBINER输入:

("GET / HTTP/1.1", {"10.31.0.1", "10.31.0.2"})

COMBINER输出:

("GET / HTTP/1.1", "2") //You have the right answer here...

减速器输入:

("GET / HTTP/1.1", {"2"}) //...but then it gets passed into the Reducer again

减速机输出:

("GET / HTTP/1.1", "1")

只有一个元素进入减速器,因此它将减小为“ 1”。

删除组合器(删除job.setCombinerClass(IntSumReducer.class);,这将起作用。

其他建议的更改:

  1. 具有Reducer输出IntWritable,而不是将数字转换为Text
  2. Set设为Set<Text>而不是Set<String>,以节省昂贵的Text -> String转换。