---恢复内容开始---

之前我们都是学习使用MapReduce处理一张表的数据(一个文件可视为一张表,hive和关系型数据库Mysql、Oracle等都是将数据存储在文件中)。但是我们经常会遇到处理多张表的场景,不同的数据存储在不同的文件中,因此Hadoop也提供了类似传统关系型数据库的join操作。Hadoop生态组件的高级框架Hive、Pig等也都实现了join连接操作,编写类似SQL的语句,就可以在MapReduce中运行,底层的实现也是基于MapReduce。本文介绍如何使用MapReduce实现join操作,为以后学习hive打下基础。

1、Map端连。
数据在进入到map函数之前就进行连接操作。适用场景:一个文件比较大,一个文件比较小,小到可以加载到内存中。如果两个都是大文件,就会出现OOM内存溢出的异常。实现Map端连接操作需要用到Job类的addCacheFile()方法将小文件分发到各个计算节点,然后加载到节点的内存中。

下面通过一个例子来实现Map端join连接操作:
1、雇员employee表数据如下:
name gender age dept_no
Tom male 30 1
Tony male 35 2
Lily female 28 1
Lucy female 32 3

2、部门表dept数据如下:
dept_no dept_name
1 TSD
2 MCD
3 PPD

代码实现如下:

 package join;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.io.*;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.Mapper; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.fs.Path; public class MapJoin extends Configured implements Tool { public static class MapJoinMapper extends Mapper<LongWritable, Text, Text,NullWritable> {
private Map<Integer, String> deptData = new HashMap<Integer, String>(); @Override
protected void setup(Mapper<LongWritable, Text, Text,NullWritable>.Context context) throws IOException, InterruptedException {
super.setup(context);
//从缓存的中读取文件。
Path[] files = context.getLocalCacheFiles();
// Path file1path = new Path(files[0]);
BufferedReader reader = new BufferedReader(new FileReader(files[0].toString()));
String str = null;
try {
// 一行一行读取
while ((str = reader.readLine()) != null) {
// 对缓存中的数据以" "分隔符进行分隔。
String[] splits = str.split(" ");
// 把需要的数据放在Map中。注意不能操作Map的大小,否则会出现OOM的异常
deptData.put(Integer.parseInt(splits[0]), splits[1]);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
reader.close();
}
} @Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text,NullWritable>.Context context) throws IOException,
InterruptedException {
// 获取从HDFS中加载的表
String[] values = value.toString().split(" ");
// 获取关联字段depNo,这个字段是关键
int depNo = Integer.parseInt(values[3]);
// 根据deptNo从内存中的关联表中获取要关联的属性depName
String depName = deptData.get(depNo);
String resultData = value.toString() + " " + depName;
// 将数据通过context写入到Reduce中。
context.write(new Text(resultData),NullWritable.get());
}
} public static class MapJoinReducer extends Reducer<Text,NullWritable,Text,NullWritable> {
public void reduce(Text key, Iterable<NullWritable> values,Context context)throws IOException,InterruptedException{
context.write(key,NullWritable.get());
}
} @Override
public int run(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Total Sort app");
//将小表加载到缓存中。
job.addCacheFile(new URI(args[0]));
job.setJarByClass(MapJoinMapper.class);
//1.1 设置输入目录和设置输入数据格式化的类
FileInputFormat.setInputPaths(job,new Path(args[1]));
job.setInputFormatClass(TextInputFormat.class); //1.2 设置自定义Mapper类和设置map函数输出数据的key和value的类型
job.setMapperClass(MapJoinMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class); //1.3 设置reduce数量
job.setNumReduceTasks(1);
//设置实现了reduce函数的类
job.setReducerClass(MapJoinReducer.class); //设置reduce函数的key值
job.setOutputKeyClass(Text.class);
//设置reduce函数的value值
job.setOutputValueClass(NullWritable.class); // 判断输出路径是否存在,如果存在,则删除
Path mypath = new Path(args[2]);
FileSystem hdfs = mypath.getFileSystem(conf);
if (hdfs.isDirectory(mypath)) {
hdfs.delete(mypath, true);
} FileOutputFormat.setOutputPath(job, new Path(args[2])); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args)throws Exception{ int exitCode = ToolRunner.run(new MapJoin(), args);
System.exit(exitCode);
}
}

执行脚本文件如下::

 /usr/local/src/hadoop-2.6./bin/hadoop jar MapJoin.jar \
hdfs://hadoop-master:8020/data/dept.txt \
hdfs://hadoop-master:8020/data/employee.txt \
hdfs://hadoop-master:8020/mapjoin_output

运行结果:

Lily female 28 1 TSD
Lucy female 32 3 PPD
Tom male 30 1 TSD
Tony male 35 2 MCD

2、Reduce端连接(Reduce side join)。
数据在Reduce进程中执行连接操作。实现思路:在Map进程中对来自不同表的数据打上标签,例如来自表employee的数据打上a标签,来自文件dept表的数据打上b标签。然后在Reduce进程,对同一个key,来自不同表的数据进行笛卡尔积操作。请看下图,我们对表employee和表dept的dept_no字段进行关联,将dept_no字段当做key。

在MapReduce中,key相同的数据会放在一起,因此我们只需在reduce函数中判断数据是来自哪张表,来自相同表的数据不进行join。

代码如下:

 public class ReduceJoin extends Configured implements Tool {
public static class JoinMapper extends
Mapper<LongWritable,Text,Text,Text> {
String employeeValue = "";
protected void map(LongWritable key, Text value, Context context)
throws IOException,InterruptedException {
/*
* 根据命令行传入的文件名,判断数据来自哪个文件,来自employee的数据打上a标签,来自dept的数据打上b标签
*/
String filepath = ((FileSplit)context.getInputSplit()).getPath().toString();
String line = value.toString();
if (line == null || line.equals("")) return; if (filepath.indexOf("employee") != -1) {
String[] lines = line.split(" ");
if(lines.length < 4) return; String deptNo = lines[3];
employeeValue = line + " a";
context.write(new Text(deptNo),new Text(employeeValue));
} else if(filepath.indexOf("dept") != -1) {
String[] lines = line.split(" ");
if(lines.length < 2) return;
String deptNo = lines[0];
context.write(new Text(deptNo), new Text(line + " b"));
}
}
} public static class JoinReducer extends
Reducer<Text, Text, Text, NullWritable> {
protected void reduce(Text key, Iterable<Text> values,
Context context) throws IOException, InterruptedException{
List<String[]> lista = new ArrayList<String[]>();
List<String[]> listb = new ArrayList<String[]>(); for(Text val:values) {
String[] str = val.toString().split(" ");
//最后一位是标签位,因此根据最后一位判断数据来自哪个文件,标签为a的数据放在lista中,标签为b的数据放在listb中
String flag = str[str.length -1];
if("a".equals(flag)) {
//String valueA = str[0] + " " + str[1] + " " + str[2];
lista.add(str);
} else if("b".equals(flag)) {
//String valueB = str[0] + " " + str[1];
listb.add(str);
}
} for (int i = 0; i < lista.size(); i++) {
if (listb.size() == 0) {
continue;
} else {
String[] stra = lista.get(i);
for (int j = 0; j < listb.size(); j++) {
String[] strb = listb.get(j);
String keyValue = stra[0] + " " + stra[1] + " " + stra[2] + " " + stra[3] + " " + strb[1];
context.write(new Text(keyValue), NullWritable.get());
}
}
}
}
} @Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
GenericOptionsParser optionparser = new GenericOptionsParser(conf, args);
conf = optionparser.getConfiguration();
Job job = Job.getInstance(conf, "Reduce side join");
job.setJarByClass(ReduceJoin.class);
//1.1 设置输入目录和设置输入数据格式化的类
//FileInputFormat.setInputPaths(job,new Path(args[0]));
FileInputFormat.addInputPaths(job, conf.get("input_data")); job.setInputFormatClass(TextInputFormat.class); //1.2 设置自定义Mapper类和设置map函数输出数据的key和value的类型
job.setMapperClass(JoinMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); //1.3 设置reduce数量
job.setNumReduceTasks(1);
//设置实现了reduce函数的类
job.setReducerClass(JoinReducer.class); //设置reduce函数的key值
job.setOutputKeyClass(Text.class);
//设置reduce函数的value值
job.setOutputValueClass(NullWritable.class); // 判断输出路径是否存在,如果存在,则删除
Path output_dir = new Path(conf.get("output_dir"));
FileSystem hdfs = output_dir.getFileSystem(conf);
if (hdfs.isDirectory(output_dir)) {
hdfs.delete(output_dir, true);
} FileOutputFormat.setOutputPath(job, output_dir); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args)throws Exception{
int exitCode = ToolRunner.run(new ReduceJoin(), args);
System.exit(exitCode);
}
}

执行MapReduce的shell脚本如下:

 /usr/local/src/hadoop-2.6./bin/hadoop jar ReduceJoin.jar \
-Dinput_data=hdfs://hadoop-master:8020/data/dept.txt,hdfs://hadoop-master:8020/data/employee.txt \
-Doutput_dir=hdfs://hadoop-master:8020/reducejoin_output

总结:
1、Map side join的运行速度比Reduce side join快,因为Reduce side join在shuffle阶段会消耗大量的资源。Map side join由于把小表放在内存中,所以执行效率很高。
2、当有一张表的数据很小时,小到可以加载到内存中,那么建议使用Map side join。

欢迎关注本人公众号了解更多关于大数据方面的知识:

一起学Hadoop——实现两张表之间的连接操作的更多相关文章

  1. EF Core中如何正确地设置两张表之间的关联关系

    数据库 假设现在我们在SQL Server数据库中有下面两张表: Person表,代表的是一个人: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ...

  2. mysql 如何找出两张表之间的关系

    分析步骤: #1.先站在左表的角度去找 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id) #2.再站在右表的角度去找 是否右表的 ...

  3. JS之document例题讲解1(两张表之间数据转移、日期时间选择、子菜单下拉、用div做下拉菜单、事件总结)

    作业一:两个列表之间数据从一个列表移动到另一个列表 <div style="width:600px; height:500px; margin-top:20px"> & ...

  4. 关于跨DB增量(增、改)同步两张表的数据小技巧

    有些场景下,需要隔离不同的DB,彼此DB之间不能互相访问,但实际的业务场景又需要从A DB访问B DB的情形,这时怎么办?我认为有如下常规的三种方案: 1.双方提供RESET API,需要访问不同DB ...

  5. MySQL实现两张表数据的同步

    有两张表A和B,要求往A里面插入一条记录的同时要向B里面也插入一条记录,向B里面插入一条记录的同时也向A插入一条记录.两张表的结构不同,需要将其中几个字段对应起来.可以用下面的触发器实现. 表A的触发 ...

  6. Oracle 两个表之间更新的实现

    Oracle 两个表之间更新的实现   来源:互联网 作者:佚名 时间:2014-04-23 21:39 Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过.查了资料,S ...

  7. SQLSERVER中如何快速比较两张表的不一样

    SQLSERVER中如何快速比较两张表的不一样 不知不觉要写2014年的最后一篇博文了~ 一般来说,如何检测两张表的内容是否一致,体现在复制的时候发布端和订阅端的两端的数据上面 我这里罗列了一些如何从 ...

  8. CROSS JOIN连接用于生成两张表的笛卡尔集

    将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘     CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...

  9. SQLServer两张表筛选相同数据和不同数据

    概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...

随机推荐

  1. iperf3 不支持双工模式

    iperf 2.05的时候,客户端可以使用参数"-d"来进行双工测试,先测试发送,client向server发送数据,等到测试时间结束后(默认为10s,可以通过-t选项来更改),然 ...

  2. MariaDB主从备份记录

    一.预期效果: 环境: centos 6.5   mariadb 10.0.14 (mysql -V) 主服务器:192.168.5.206   从服务器:192.168.5.207   主服务器数据 ...

  3. 技术的热门度曲线:GHC

      全球最大的 IT 咨询公司高德纳(Gartner),有一个"技术热门度曲线"模型(Gartner Hype Cycle). 该模型认为,一门技术的发展要经历五个阶段. (1)启 ...

  4. Vue.js结合vue-router和webpack编写单页路由项目

    一.前提 1. 安装了node.js. 2. 安装了npm. 3. 检查是否安装成功: 打开cmd,输入node,没有报“node不是内部或外部命令”表示安装成功node.js. 打开cmd,输入np ...

  5. Android 自动化测试框架

    Android常用的自动化测试工具框架: Monkey,MonkeyRunner,UIAutomator,Robotium,Appium,Monkey Talk...... 但这些工具框架都是什么呢有 ...

  6. NIO(一)

    1.NIO是什么? 是JDK1.4之后推出的一个新的IO操作(netty.mina通讯框架的底层都是NIO实现的连接) 2.NIO和IO的区别(阻塞只会出现在网络通讯中,都是同步) NIO:非阻塞类型 ...

  7. mysql数据库之基本操作和存储引擎

    一.知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理系统:如mysql,是一个软件 数据库:oldboy_stu,相当于文件夹 表:student,scholl,class_list ...

  8. SpringBoot图片上传(二)

    需求简介:做新增的时候,需要上传图片.(⊙o⊙)…这需求描述也太简单了吧,限制文件大小60*60 512kb ,第一次做,记录一下嗷,废话就不啰嗦了 上代码 代码: //html代码<div c ...

  9. 【python】mongo删除数据

    参考:https://stackoverflow.com/questions/23334743/setting-justone-limiter-for-pymongo-remove-throws-ty ...

  10. django----注意事项

    不用带参数 必须要带参数: