MapReduce_Join示例

Join示例:

需求:两个文件student_info.txt和student_class_info.txt

student_info.txt:

Amy 00001

Tom 00002

Binder 00003


student_class_info.txt

00001 Chinese

00002 Math

00003 English


要求输出:

Amy Chinese

Tom Math

Binder English


程序示例:

package com.xfyan.MR.two;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

/**
 *student_info:
 *Amy 00001
 *Tom 00002
 *Binder 00003
 *
 *student_class_info:
 *00001 Chinese
 *00002 Math
 *00003 English
 *
 * print:
 * Amy Chinese
 * Tom Math
 * Binder English
 *
 */
public class Join {
	public static final String LEFT_FILENAME = "student_info.txt";
	public static final String RIGHT_FILENAME = "student_class_info.txt";
	public static final String LEFT_FLAG = "l";
	public static final String RIGHT_FLAG = "r";
	
	public class JoinMapper extends Mapper<LongWritable, Text, Text, Text>{
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			String filePath = ((FileSplit)context.getInputSplit()).getPath().toString();
			String fileFlag = null;
			String joinKey = null;
			String joinValue = null;
			
			if(filePath.contains(LEFT_FILENAME)){
				fileFlag = LEFT_FLAG;
				joinKey = value.toString().split("\t")[1];
				joinValue = value.toString().split("\t")[0];
			}else{
				fileFlag = RIGHT_FLAG;
				joinKey = value.toString().split("\t")[0];
				joinValue = value.toString().split("\t")[1];
			}
			
			context.write(new Text(joinKey),new Text(joinValue + "\t" + fileFlag));
		}
	}
	
	
	public class JoinReducer extends Reducer<Text, Text, Text, Text>{
		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			Iterator<Text> it = values.iterator();
			
			List<String> studentClassNames = new ArrayList<String>();
			String studentName ="";
			while(it.hasNext()){
				String[] infos = it.next().toString().split("\t");
				if(infos[1].equals(LEFT_FLAG)){
					studentName = infos[0];
				}else{
					studentClassNames.add(infos[0]);
				}
			}
			
			for(int i = 0; i < studentClassNames.size();i++){
				context.write(new Text(studentName),new Text(studentClassNames.get(i)));
			}
		}
	}
	
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		
		Job job = new Job(conf,"Join");
		job.setJarByClass(Join.class);
		
		FileInputFormat.addInputPath(job,new Path(args[0]));
		FileOutputFormat.setOutputPath(job,new Path(args[1]));
		
		job.setMapperClass(JoinMapper.class);
		job.setReducerClass(JoinReducer.class);
		job.setOutputFormatClass(TextOutputFormat.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		System.exit(job.waitForCompletion(true)?0:1);
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce中实现join操作通常有两种方式:Reduce-side join和Map-side join。 1. Reduce-side join: Reduce-side join是最常用的实现方式。它的基本思想是将两个需要join的表分别映射为(key, value)的形式,其中key为需要join的字段,value则包含该字段以及其他需要输出的字段。然后将两个表的数据都输入到Map函数中,在Map函数中对两个表的数据进行标记,并将需要join的字段作为输出的key。在Reduce函数中,对相同的key进行合并,得到最终的输出结果。 下面是一个示例的Reduce-side join实现: Map函数: ``` public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split(","); String joinKey = fields[0]; String table = fields[1]; // 表名 String data = fields[2]; // 数据 Text outputKey = new Text(joinKey); Text outputValue = new Text(table + ":" + data); context.write(outputKey, outputValue); } ``` Reduce函数: ``` public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { List<String> table1Data = new ArrayList<String>(); List<String> table2Data = new ArrayList<String>(); for (Text value : values) { String[] fields = value.toString().split(":"); if (fields[0].equals("table1")) { table1Data.add(fields[1]); } else if (fields[0].equals("table2")) { table2Data.add(fields[1]); } } for (String data1 : table1Data) { for (String data2 : table2Data) { context.write(key, new Text(data1 + "," + data2)); } } } ``` 2. Map-side join: Map-side join是一种更加高效的实现方式,它的基本思想是将一个表的数据缓存到内存中,然后在Map函数中将另一个表的数据与缓存的数据进行join。需要注意的是,Map-side join只适用于小表与大表之间的join操作,因为需要将小表的数据全部缓存到内存中。 下面是一个示例的Map-side join实现: Map函数: ``` public void setup(Context context) throws IOException, InterruptedException { // 读取小表的数据并缓存到内存中 BufferedReader br = new BufferedReader(new FileReader("table1.csv")); String line; while ((line = br.readLine()) != null) { String[] fields = line.split(","); String joinKey = fields[0]; String data = fields[1] + "," + fields[2]; table1Data.put(joinKey, data); } br.close(); } public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split(","); String joinKey = fields[0]; String data = fields[1] + "," + fields[2]; if (table1Data.containsKey(joinKey)) { String table1Data = table1Data.get(joinKey); context.write(new Text(joinKey), new Text(table1Data + "," + data)); } } ``` 需要注意的是,Map-side join需要提前将小表的数据缓存到内存中,因此需要在Map函数之前执行setup函数。同时,为了提高效率,通常使用HashMap等数据结构来缓存小表的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值