【Mapreduce】從程式碼上解決Output directory already exists錯誤,避免每次除錯都要手動刪除輸出資料夾
阿新 • • 發佈:2019-01-22
Mapreduce除錯很蛋疼的,它不會覆蓋上一次輸出的結果,如果發現輸出資料夾已經存在,比如我的除錯輸出資料夾是hdfs://192.168.230.129:9000/output,它會直接給你報如下錯誤:
Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://192.168.230.129:9000/output already exists at org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.checkOutputSpecs(FileOutputFormat.java:123) at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:770) at org.apache.hadoop.mapreduce.Job.submit(Job.java:432) at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:447) at MyMapReduce.main(MyMapReduce.java:65)
如下圖所示:
當然,錯誤很明瞭,就是輸出資料夾已存在。
不過網上有寫很坑爹的教程,表示解決這個錯誤,要自己手動刪除輸出資料夾。
這很蛋疼,無論你這次除錯成功還是報錯與否,都要先重新整理HDFS,再刪除,再執行程式:
這是何其地蛋疼啊!其實可以在程式碼上利用hdfs的檔案操作,解決這個問題。思想就是在程式碼執行之前,也就是提交作業之前,判斷output資料夾是否存在,如果存在則刪除。具體程式碼如下:
import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class MyMapReduce { public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args) .getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job = new Job(conf); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 判斷output資料夾是否存在,如果存在則刪除 Path path = new Path(otherArgs[1]);// 取第1個表示輸出目錄引數(第0個引數是輸入目錄) FileSystem fileSystem = path.getFileSystem(conf);// 根據path找到這個檔案 if (fileSystem.exists(path)) { fileSystem.delete(path, true);// true的意思是,就算output有東西,也一帶刪除 } FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
關鍵就是如下這4行:
// 判斷output資料夾是否存在,如果存在則刪除
Path path = new Path(otherArgs[1]);// 取第1個表示輸出目錄引數(第0個引數是輸入目錄)
FileSystem fileSystem = path.getFileSystem(conf);// 根據path找到這個檔案
if (fileSystem.exists(path)) {
fileSystem.delete(path, true);// true的意思是,就算output有東西,也一帶刪除
}
教會Mapreduce這SB覆蓋上一次執行結果,別隻會在這報錯!