您的位置:首页 > 运维架构

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

2013-10-13 16:55 399 查看
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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: