有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Map reduce驱动程序代码不工作

我正在自学大数据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);

然后它就正常工作了

我可以知道这些电话的原因和用途吗


共 (2) 个答案

  1. # 1 楼答案

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

    Path outputPath = new Path(args[1]);
    if (outputPath.getFileSystem(conf).exist()) {
    outputPath.getFileSystem(conf).delete(outputPath);
    }
    
  2. # 2 楼答案

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

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

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

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