代码自动删除ouput输出目录


title: 代码自动删除ouput输出目录
date: 2023-10-04 13:35:49
categories:

  • mapreduce
    tags:

添加你需要先判断的目录即可

		FileSystem.get(conf).delete(new Path(args[1]), true);

或者

args = new String[] { "inputdatas/ch07/totalsort", "outUserPartition1", "outUserPartition2" };

		// 检查并删除可能已存在的输出目录

	    Path outputDir1 = new Path(args[1]);

	    Path outputDir2 = new Path(args[2]);

	    Configuration conf = new Configuration();

	    FileSystem fs = FileSystem.get(conf);

	    if (fs.exists(outputDir1)) {

	        fs.delete(outputDir1, true); // 递归删除目录及其内容

	    }

	    if (fs.exists(outputDir2)) {

	        fs.delete(outputDir2, true); // 递归删除目录及其内容

	    }
package ch07.totalsort.partition;



import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.conf.Configured;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.*;

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.input.KeyValueTextInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.mapreduce.lib.partition.InputSampler;

import org.apache.hadoop.mapreduce.lib.partition.TotalOrderPartitioner;

//import org.apache.hadoop.mapreduce.lib.partition.InputSampler.SplitSampler;

import org.apache.hadoop.util.Tool;

import org.apache.hadoop.util.ToolRunner;



import java.io.IOException;



//删除存在的目录

import org.apache.hadoop.fs.FileSystem;





public class TotalOrderingPartition extends Configured implements Tool {

	static class SimpleMapper extends Mapper<Text, Text, Text, IntWritable> {

		@Override

		protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {

			IntWritable intWritable = new IntWritable(Integer.parseInt(key.toString()));

			context.write(key, intWritable);

		}

	}



	static class SimpleReducer extends Reducer<Text, IntWritable, IntWritable, NullWritable> {

		@Override

		protected void reduce(Text key, Iterable<IntWritable> values, Context context)

				throws IOException, InterruptedException {

			for (IntWritable value : values)			

				context.write(value, NullWritable.get());

			      

		}

		

	}



/*	

	 * public static class KeyComparator extends WritableComparator { protected

	 * KeyComparator() { super(Text.class, true); }

	 * 

	 * @Override public int compare(WritableComparable w1, WritableComparable w2) {

	 * int v1 = Integer.parseInt(w1.toString()); int v2 =

	 * Integer.parseInt(w2.toString());

	 * 

	 * return v1 - v2; } }

	 */

	@Override

	public int run(String[] args) throws Exception {

		Configuration conf = getConf();

		Job job = Job.getInstance(conf, "Total Order Sorting");

		job.setJarByClass(TotalOrderingPartition.class);

		job.setInputFormatClass(KeyValueTextInputFormat.class);//取简直对

		// job.setSortComparatorClass(KeyComparator.class);

		

		



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

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

		job.setNumReduceTasks(3);

		job.setMapOutputKeyClass(Text.class);

		job.setMapOutputValueClass(IntWritable.class);

		job.setOutputKeyClass(IntWritable.class);

		job.setOutputValueClass(NullWritable.class);



		//抽样排序,系统自动平衡,临时的处理数据需要的地方

		

		TotalOrderPartitioner.setPartitionFile(job.getConfiguration(), new Path(args[2]));

//		 InputSampler.Sampler<Text, Text> sampler = new

//		 InputSampler.RandomSampler<>(0.01, 1000, 100);

		InputSampler.Sampler<Text, Text> sampler = new InputSampler.SplitSampler<Text, Text>(10000, 10);

		InputSampler.writePartitionFile(job, sampler);



		job.setPartitionerClass(TotalOrderPartitioner.class);

		job.setMapperClass(SimpleMapper.class);

		job.setReducerClass(SimpleReducer.class);



		job.setJobName("iteblog");



		return job.waitForCompletion(true) ? 0 : 1;

	}



	public static void main(String[] args) throws Exception {

		args = new String[] { "inputdatas/ch07/totalsort", "outUserPartition1", "outUserPartition2" };

		// 检查并删除可能已存在的输出目录

	    Path outputDir1 = new Path(args[1]);

	    Path outputDir2 = new Path(args[2]);

	    Configuration conf = new Configuration();

	    FileSystem fs = FileSystem.get(conf);

	    if (fs.exists(outputDir1)) {

	        fs.delete(outputDir1, true); // 递归删除目录及其内容

	    }

	    if (fs.exists(outputDir2)) {

	        fs.delete(outputDir2, true); // 递归删除目录及其内容

	    }



		int exitCode = ToolRunner.run(new TotalOrderingPartition(), args);

		System.exit(exitCode);

	}

}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elik-hb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值