MapReduce編程實例(六)

前提準備:

1.hadoop安裝運行正常。Hadoop安裝配置請參考:Ubuntu下 Hadoop 1.2.1 配置安裝

2.集成開發環境正常。集成開發環境配置請參考 :Ubuntu 搭建Hadoop源碼閱讀環境


MapReduce編程實例:

MapReduce編程實例(一),詳細介紹在集成環境中運行第一個MapReduce程序 WordCount及代碼分析

MapReduce編程實例(二),計算學生平均成績

MapReduce編程實例(三),數據去重

MapReduce編程實例(四),排序

MapReduce編程實例(五),MapReduce實現單表關聯



多表關聯
描述:
兩張表關聯,如下:
左表:
factoryname address
BMW Factory 2
Benz Factory 3
Voivo Factory 4
LG Factory 5

右表:
addressID addressname
2 Beijing
3 Guangzhou
4 Shenzhen
5 Sanya

根據addressID關聯求出factoryname-address表。很明顯,左右關聯即可,和單表關聯一樣。不多作表述,有需要可以查看單表關聯的分析。

package com.t.hadoop;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

/**
 * 多表排序
 * @author daT [email protected]
 *
 */
public class MTJoin {
	public static int times = 1;
	
	public static class MTMapper extends Mapper<Object, Text, Text, Text>{

		@Override
		protected void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			String relation = new String();
			String line = value.toString();
			if(line.contains("factoryname")||line.contains("addressID")) return;
			int i = 0;
			while(line.charAt(i)<'0'||line.charAt(i)>'9'){
				i++;
			}
			if(i>0){//左表
				relation = "1";
				context.write(new Text(String.valueOf(line.charAt(i))),new Text(relation + line.substring(0,i-1)));
			}else{//右表
				relation = "2";
				context.write(new Text(String.valueOf(line.charAt(i))),new Text(relation +line.substring(i+1)));
			}
			
		}
		
	}
	
	
	public static class MTReducer extends Reducer<Text, Text, Text, Text>{

		@Override
		protected void reduce(Text key, Iterable<Text> value,Context context)
				throws IOException, InterruptedException {
			if(times==1){
				context.write(new Text("factoryName"), new Text("Address"));
				times ++;
			}
			int factoryNum = 0;
			int addressNum = 0;
			String[] factorys = new String[10];
			String[] addresses = new String[10];
			
			for(Text t:value){
				if(t.charAt(0)=='1'){//左表
					factorys[factoryNum]=t.toString().substring(1);
					factoryNum++;
				}else{//右表
					addresses[addressNum]=t.toString().substring(1);
					addressNum++;
				}
			}
			
			for(int i = 0;i<factoryNum;i++){
				for(int j=0;j<addressNum;j++){
					context.write(new Text(factorys[i]), new Text(addresses[j]));
				}
			}
			
		}
		
	}
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
		
		if(otherArgs.length<2){
			System.out.println("Parameters error");
			System.exit(2);
		}
		
		Job job =new Job(conf,"MTjoin");
		job.setJarByClass(MTJoin.class);
		job.setMapperClass(MTMapper.class);
		job.setReducerClass(MTReducer.class);
		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);
		
	}
}


輸出結果:
factoryName Address
BMW Factory Beijing
Benz Factory Guangzhou
Voivo Factory Shenzhen
LG Factory Sanya

歡迎同學們多多交流~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章