我的hadoop初学程序------简单数据去重--------Deduplication

本文详细介绍了数据去重程序的核心概念、实现步骤及关键类的解释,包括Map和Reduce的功能,以及如何通过配置Hadoop来运行此程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package bin;

import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 Deduplication {

//描述:数据去重程序
//数据的每一行作为一项输入,那么要知道map的功能以及reduce的功能
//map:提取出整个文件中的有用内容,即---键值对<key,value>---
//map提取出的原始的键值对,经过合并,形成同一个key的所有value集<key,[value1,value2,value3,...]>----它是作为reduce的输入的
//即,reduce的输入是一个特定类型的key,还有一个values集合。
//reduce针对每一个key处理这个values得到输出:一个<key,result>
	
/**
 * 	map类
 * @author xinxin
 *
 */
	public static class DedepMap extends Mapper<Object, Text, Text, Text>{
		private static Text line =  new Text();
		//实现map函数
		public void map(Object key,Text value,Context context) {
			line=value;
			try {
				context.write(line, new Text(""));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
	}
	
	
	/**
	 * reduce 将输入的key复制到输出数据的key上并输出
	 * @author xinxin
	 *
	 */
	public static class DeDupReduce extends Reducer<Text, Text, Text, Text>{
	
		public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
		context.write(key, new Text(""));
	}
	}
	

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration configuration= new Configuration();
		String[] otherArgs = new GenericOptionsParser(configuration,args).getRemainingArgs();
		 if (otherArgs.length !=2) {
			System.err.println("Usage: Deduplication <in> <out>");
			System.exit(2);
		}
		 Job job =new Job(configuration, "tracert Deduplication");//新建一个job,给它起个名字,以便跟踪察看任务的执行情况--都有
		 job.setJarByClass(Deduplication.class);//主类---都有
		 
		 //设置需要使用的map,combiner,reducer类
		 job.setMapperClass(DedepMap.class);
		 job.setCombinerClass(DeDupReduce.class);
		 job.setReducerClass(DeDupReduce.class);
		 
		 //设置map reduce 的输出健和输出值类型
		 job.setOutputKeyClass(Text.class);
		 job.setOutputValueClass(Text.class);
		 
		 //设置文件输入类型和文件输出类型
		 FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		 
		 System.exit(job.waitForCompletion(true)? 0 : 1);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值