MapReduce程式設計例項之倒排索引 1

Thinkgamer_gyt發表於2015-11-24
任務描述:
有一批電話清單,記錄了使用者A撥打給使用者B的記錄
做一個倒排索引,記錄撥打給使用者B所有的使用者A、

example data:

13614004876 110
18940084808 10086
13342445911 10001
13614004876 120
18940084808 1008611
13342445911 110
15847985621 10000


code:

<span style="font-size:14px;">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.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 daopaisuoyin {
	enum Counter{   
	     LINESKIP,      //出錯的行      
	 }   

	public static class Map extends Mapper<Object, Text, Text, Text>{
		
		public void map(Object key,Text value,Context context){
			String line = value.toString();
			try{
					String[] lineSplit = line.split(" ");
					String newKey = lineSplit[0];
					String newValue = lineSplit[1];
					context.write(new Text(newKey), new Text(newValue));
			}catch(Exception e){
				 context.getCounter(Counter.LINESKIP).increment(1);
				 return;
			}
		}
		
	}
	
	public static class Reduce extends Reducer<Text, Text, Text, Text>{
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
			String result = "";
			for (Text value : values) {
				result += value.toString() + " # ";
			}
			context.write(key, new Text(result));
		}
	}
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub

		Job job = new Job(new Configuration(), " 倒排索引 ");
		job.setJarByClass(daopaisuoyin.class);
		
		job.setNumReduceTasks(1);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.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.exit(result); //判斷是否結束
	}

}
</span>


結果顯示:


相關文章