关于Hadoop环境下Mapreduce解决Output directory already exists错误,避免每次调试都要手动删除输出文件夹

public class Sort {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        String[] path = {
                "hdfs://localhost:9000/user/hduser/sort/input",
                "hdfs://localhost:9000/user/hduser/sort/output"
        };
        
        Configuration conf = new Configuration();
        
        Job job = Job.getInstance(conf);
        job.setJarByClass(Sort.class);
        job.setOutputKeyClass(OutputKey.class);
        job.setOutputValueClass(OutputValue.class);
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        
        // 设置四个reduce同步执行
//        job.setNumReduceTasks(4);
        // 设置Partitioner
//        job.setPartitionerClass(MyPartitioner.class);
        
        FileInputFormat.setInputPaths(job, new Path(path[0]));
        
        FileSystem fs = FileSystem.get(conf);
        Path p = new Path(path[1]);
        if(fs.exists(p)) {
            // 目录存在,删除
            fs.delete(p, true);
        }

        FileOutputFormat.setOutputPath(job, p);
        
        boolean a = job.waitForCompletion(true);
        if(a) {
            System.exit(0);
        }
        else {
            System.exit(1);
        }
    }
}
贴出以上部分代码为例,因为之前都是每次运行完想要再次运行就需要自己手动去删除output文件夹,所以这次就直接在程序里,对HDFS的文件夹进行了操作,如果每次运行output文件夹都在,就会自动删除

代码如下     

   FileInputFormat.setInputPaths(job, new Path(path[0]));
        
        FileSystem fs = FileSystem.get(conf);
        Path p = new Path(path[1]);
        if(fs.exists(p)) {
            // 目录存在,删除
            fs.delete(p, true);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值