MapReduce程式設計例項之資料去重

Thinkgamer_gyt發表於2015-11-24

任務描述:

讓原始資料中出現次數超過一次的資料在輸出檔案中只出現一次。

example data:

2015-3-1 a
2015-3-2 b
2015-3-3 c
2015-3-4 d
2015-3-5 e
2015-3-6 f
2015-3-7 g
2015-3-1 a
2015-3-2 b
2015-3-3 c
2015-3-4 d
2015-3-5 e
2015-3-6 f
2015-3-7 g
2015-3-1 a
2015-3-2 b
2015-3-3 c
2015-3-4 d
2015-3-5 e
2015-3-6 f
2015-3-7 g

code:

package mrTest;

import java.io.IOException;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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 com.ibm.icu.text.SimpleDateFormat;

public class shujuquchong {

	public static class Map extends Mapper<Object, Text, Text, NullWritable>{
		
		public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
			context.write(value, NullWritable.get());
			}		
	}
	
	public static class Reduce extends Reducer< Text, NullWritable,  Text, NullWritable>{
		public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException{
			context.write(key,  NullWritable.get());
		}
	}
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub

		Job job = new Job(new Configuration(), " 資料去重");
		job.setJarByClass(shujuquchong.class);
		
		job.setNumReduceTasks(1);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		//記錄時間
		SimpleDateFormat  sdf = new SimpleDateFormat();
	    Date start = new Date();        //開始時間
	    
		int result = job.waitForCompletion(true)? 0 : 1;    //任務開始
		
		Date end = new Date();     //結束時間
		float time = (float)((end.getTime() - start.getTime()) / 60000.0);  //任務開始到結束經歷的時間
		
		System.out.println("Job 開始的時間為:" + start);
		System.out.println("Job 結束的時間為:" + end);
		System.out.println("Job 經歷的時間為:" + time + "分鐘");
		
		System.out.println("Job 的名字:" + job.getJobName());
		System.out.println("Job 是否成功:" + job.isSuccessful() );
		System.out.println("Job 輸入的行數:" + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter",  "MAP_INPUT_RECORDS").getValue());
		System.out.println("Job 輸出的行數:" + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter",  "MAP_OUTPUT_RECORDS").getValue());
		System.out.println("Job 輸入的行數:" + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter",  "REDUCE_INPUT_RECORDS").getValue());
		System.out.println("Job 輸出的行數:" + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter",  "REDUCE_OUTPUT_RECORDS").getValue());

		System.exit(result); //判斷是否結束
	}

}


結果展示:


相關文章