mapreduce之mapjoin

mapjoin指的是在mapreduce的map阶段先加载一个文件缓存到内存当中,这个文件可能是从磁盘读取的或网络请求的都可以。

map(key,value,context)方法中读取的数据key和value,这两个数据和先前缓存到内存中的数据一起做处理后再context.write()到reduce阶段。

mapjoin相当于在map阶段写数据到reduce阶段前对数据做了处理。

比如有两个txt文件
pd.txt
01 mac
02 huawei
03 xiaomi
第一个列代表订单id ,第二列代表商品名称

order.txt
201801 01 1
201802 02 2
201803 03 3
201804 01 4
201805 02 5
201806 03 6
第一列代表时间戳,第二列代表订单id,第三列代表数量

如果希望在map阶段输出
时间戳 订单id 订单数量 商品名称
201801 01 1 mac

可以在map中这样定义

package com.tony.mapjoin;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/*
 * 商品表加载到内存中,然后数据在map端输出前进行替换
 */
public class CacheMapper extends Mapper<LongWritable,Text,Text,NullWritable> {
	//1.商品表加入到内存
	HashMap<String,String> pdmap = new HashMap<>();
	protected void setup(Context context) throws IOException {
		//加载缓存文件
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("pd.txt"),"utf-8"));
		String line;
		while(StringUtils.isNotEmpty(line = br.readLine())) {
			//切分
			String[] fields = line.split("\t");
			
			//缓存
			pdmap.put(fields[0], fields[1]);
		}
		br.close();
	}
	
	//2.map传输
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		//获取数据
		String line = value.toString();
		
		//切割
		String[] fields = line.split("\t");
		
		//获取订单商品id
		String pid = fields[1];
		
		//根据id查找商品名称
		String pdName = pdmap.get(pid);
		
		//拼接数据
		line = line +"\t" +pdName;
		
		//输出
		context.write(new Text(line), NullWritable.get());
	}
}

在自定义driver驱动类中

public class CacheDriver {
	public static void main(String[] args) throws Exception {
		//1.获取job信息
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		
		//2.获取jar包
		job.setJarByClass(CacheDriver.class);
		
		//3.获取自定义的mapper与reducer类
		job.setMapperClass(CacheMapper.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		FileInputFormat.setInputPaths(job, new Path("e://bigdata//table0112//in"));
		FileOutputFormat.setOutputPath(job, new Path("e://bigdata//table0112//out"));
		
		//加载缓存商品数据
		job.addCacheFile(new URI("file:///e:/bigdata/table0112/inputfile/pd.txt"));
	
		//设置一下reducetask的数量,因为没有设置reduce类
		job.setNumReduceTasks(0);
		
		//提交任务
		boolean rs = job.waitForCompletion(true);
		System.out.println(rs?0:1);
		
	}
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MapReduce 中实现两个表的 join 操作通常需要以下步骤: 1. Mapper 阶段:将两个表中需要进行 join 的列作为 key,将整行数据作为 value 发送给 reduce 阶段。需要注意的是,对于需要 join 的列,需要在 Mapper 中进行标记。 2. Partitioner 阶段:将 Mapper 阶段输出的数据按照 key 进行分区,使得同一个 key 的数据被分配到同一个 reduce task 中。 3. Sort/Shuffle 阶段:对于每个 reduce task,将其对应的数据按照 key 进行排序,使得同一个 key 的数据排在一起。 4. Reducer 阶段:对于每个 key,将其对应的两个表的数据进行合并,并将结果输出。 以下是一个示例代码: ```java public class JoinMapper extends Mapper<LongWritable, Text, Text, Text> { private Text joinKey = new Text(); private Text joinValue = new Text(); protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] fields = value.toString().split("\t"); String joinColumnName = "join_column_name"; String tableIndicator = fields[0]; if (tableIndicator.equals("table1")) { joinKey.set(fields[1]); joinValue.set(joinColumnName + "\t" + fields[2]); context.write(joinKey, joinValue); } else if (tableIndicator.equals("table2")) { joinKey.set(fields[2]); joinValue.set(joinColumnName + "\t" + fields[1]); context.write(joinKey, joinValue); } } } public class JoinReducer extends Reducer<Text, Text, Text, Text> { private Text outputValue = new Text(); protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String joinColumnName = "join_column_name"; String table1Value = null; String table2Value = null; for (Text value : values) { String[] fields = value.toString().split("\t"); if (fields[0].equals(joinColumnName)) { if (table1Value == null) { table1Value = fields[1]; } else { table2Value = fields[1]; } } } if (table1Value != null && table2Value != null) { outputValue.set(table1Value + "\t" + table2Value); context.write(key, outputValue); } } } ``` 在上述示例中,我们假设有两个表,分别为 table1 和 table2,需要按照它们的某一列进行 join 操作。Mapper 阶段将需要 join 的列作为 key,将整行数据作为 value 发送给 reduce 阶段;Partitioner 阶段按照 key 进行分区;Sort/Shuffle 阶段对于每个 reduce task,将其对应的数据按照 key 进行排序;Reducer 阶段对于每个 key,将其对应的两个表的数据进行合并,并将结果输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值