映射减少驱动程序代码不工作

Map reduce Driver Code not Working

我正在自学大数据Hadoop,我写了一个简单的Map Reduce代码 字数统计哪个不行,请看一下

// importing all classes

public class WordCount {

public static class Map extends
        Mapper<LongWritable, Text, Text, IntWritable> {
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String Line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(Line);
        while (tokenizer.hasMoreTokens()) {
            value.set(tokenizer.nextToken());
            context.write(value, new IntWritable(1));
        }
    }
}

public static class Reduce extends
        Reducer<Text, IntWritable, Text, IntWritable> {

    public void reduce(Text key, Iterable<IntWritable> values,
            Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable x : values) {
            sum = sum + x.get();
        }
        context.write(key, new IntWritable(sum));
    }

}

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = new Job(conf, "Word Count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

}

}

但是在替换这些行之后

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

来自这些

的驱动程序代码
    Path outputPath = new Path(args[1]);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    outputPath.getFileSystem(conf).delete(outputPath);

    System.exit(job.waitForCompletion(true) ? 0 : 1);

然后它就可以正常工作了。

我可以知道原因以及这些行的用途吗?

你能告诉我你之前遇到了什么错误吗? 这个错误可能与输出路径已经存在有关。 在这里,您编写了每次都会删除输出路径的代码。 如果输出路径已经存在,那么它将删除该路径。 你可以这样写这段代码。

Path outputPath = new Path(args[1]);
if (outputPath.getFileSystem(conf).exist()) {
outputPath.getFileSystem(conf).delete(outputPath);
}

我假设当你说它不工作时,你会得到以下错误:-

org.apache.hadoop.mapred.FileAlreadyExistsException: **Output directory hdfs://localhost:54310/<<your_output_directory>> already exists**

在提交 map reduce 作业之前,输出目录不应存在。所以它可能给了你上面的异常。

您在驱动程序中使用的新代码行从路径获取文件系统(local/hdfs 基于 conf 对象)并在提交 map reduce 作业之前删除输出路径。所以现在作业执行,因为输出目录不存在。