最近在准备抽取数据的工作。有一个id集合200多M,要从另一个500GB的数据集合中抽取出所有id集合中包含的数据集。id数据集合中每一个行就是一个id的字符串(Reduce side join要在每行的行尾加“,”号,而Map side join不必,如果加了也可以处理掉),类似,500GB的数据集合中每一行是某一id对应的全记录,用“,”号分隔。

为什么不在hive或者pig下面搞这个操作呢?主要是因为Hive配置了Kerberos认证之后,还有一个问题没有解决,包含metastore的主机无法从namenode主机获取票据,所以就暂时放一放吧。用MapReduce来搞吧。在Hive下比较方便,但在MapReduce中实现就比较麻烦。

1、概述

在传统数据库(如:MySql)中,JOIN操作常常是非常耗时的。而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的技巧。下面分别介绍MapReduce中的几种常见join,比如有最常见的 map side join,reduce side join,semi join(这些在Hive中都有) 等。Map side join在处理多个小表关联大表时非常有用,而 reduce join 在处理多表关联时是比较麻烦的,会造成大量的网络IO,效率低下,但在有些时候也是非常有用的。

2. 常见的join方法介绍

2.1 map side join

Map side   join是针对以下场景进行的优化:两个待连接表中,有一个表非常大,而另一个表非常小,以至于小表可以直接存放到内存中。这样,我们可以将小表复制多份,让每个map须   task内存中存在一份(比如存放到hash table中),然后只扫描大表:对于大表中的每一条记录key/value,在hash   table中查找是否有相同的key的记录,如果有,则连接后输出即可。

为了支持文件的复制,Hadoop提供了一个类DistributedCache,使用该类的方法如下:

(1)用户使用静态方法DistributedCache.addCacheFile()指定要复制的文件,它的参数是文件的URI(如果是HDFS上的文件,可以这样:hdfs://namenode:9000/home/XXX/file,其中9000是自己配置的NameNode端口号)。JobTracker在作业启动之前会获取这个URI列表,并将相应的文件拷贝到各个TaskTracker的本地磁盘上。(2)用户使用DistributedCache.getLocalCacheFiles()方法获取文件目录,并使用标准的文件读写API读取相应的文件。

package com.unionpayadvisors;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
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.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.hsqldb.lib.StringUtil; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class AccountTableJoin extends Configured implements Tool {
private static WriteLog log = WriteLog.getInstance(); private static String parseRaw(String str) {
if (StringUtil.isEmpty(str)) {
return str;
}
str = str.trim();
if (str.startsWith("\"")) {
str = str.substring(1);
}
if (str.endsWith("\"")) {
str = str.substring(0, str.length() - 1);
}
return str.trim();
} public static class MapClass extends
Mapper<LongWritable, Text, Text, NullWritable> {
// 用于缓存 user_account 中的数据 private Set<String> accountSet = new HashSet<String>(); private Text accKey = new Text();
private NullWritable nullValue = NullWritable.get();
private String[] kv; private Jedis jedis = new JedisPool("192.168.2.101", 6379).getResource();
// 此方法会在map方法执行之前执行
// @Override
protected void setup(Context context) throws
IOException,InterruptedException {
BufferedReader in = null;
try {
// 从当前作业中获取要缓存的文件 Path[] paths = DistributedCache.getLocalCacheFiles(context .getConfiguration()); String accountLine = null; for (Path path : paths) { if (path.toString().contains("account")) { in = new BufferedReader(new FileReader(path.toString())); while (null != (accountLine = in.readLine())) {
log.logger("AccountTableJoin",
"accountSet="+parseRaw(accountLine.split(",", -1)[0]));
accountSet.add(parseRaw(accountLine.split(",", -1)[0])); } } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { kv = value.toString().split(","); //map join: 在map阶段过滤掉不需要的数据
if(kv.length==4&&accountSet.contains(parseRaw(kv[0]))){
accKey.set(value);
context.write(accKey, nullValue);
} }
} public int run(String[] args) throws Exception { log.logger("XXXXXXXXX", "begin in"); Job job = new Job(getConf(), "AccountTableJoin"); job.setJobName("AccountTableJoin"); job.setJarByClass(AccountTableJoin.class); job.setMapperClass(MapClass.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
String[] otherArgs = new GenericOptionsParser(job.getConfiguration(),
args).getRemainingArgs(); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new AccountTableJoin(),
args);
System.exit(res); } /*
* hadoop jar AccountTableJoin.jar AccountTableJoin
* /user/he/sample_account.del /user/he/SAMPLE_SUM_2012070809101112.del
* /user/he/ACCOUNT_JOIN_RESULT
*/
}

WriteLog代码:

package com.unionpayadvisors;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Calendar; public class WriteLog {
/**写日志<br>
* 写logString字符串到./log目录下的文件中
* @param logString 日志字符串
* @author tower
*/ private static WriteLog instance = null;
private WriteLog(){};
public static WriteLog getInstance() {
if( instance == null ) {
instance = new WriteLog();
}
return instance;
} public void logger(String fileNameHead,String logString) {
try {
String logFilePathName=null;
Calendar cd = Calendar.getInstance();//日志文件时间
int year=cd.get(Calendar.YEAR);
String month=addZero(cd.get(Calendar.MONTH)+1);
String day=addZero(cd.get(Calendar.DAY_OF_MONTH));
String hour=addZero(cd.get(Calendar.HOUR_OF_DAY));
String min=addZero(cd.get(Calendar.MINUTE));
String sec=addZero(cd.get(Calendar.SECOND)); File fileParentDir=new File("./log");//判断log目录是否存在
if (!fileParentDir.exists()) {
fileParentDir.mkdir();
}
if (fileNameHead==null||fileNameHead.equals("")) {
logFilePathName="./log/"+year+month+day+hour+".log";//日志文件名
}else {
logFilePathName="./log/"+fileNameHead+year+month+day+hour+".log";//日志文件名
} PrintWriter printWriter=new PrintWriter(new FileOutputStream(logFilePathName, true));//紧接文件尾写入日志字符串
String time="["+year+month+day+"-"+hour+":"+min+":"+sec+"] ";
printWriter.println(time+logString);
printWriter.flush(); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.getMessage();
}
} /**整数i小于10则前面补0
* @param i
* @return
* @author tower
*/
public static String addZero(int i) {
if (i<10) {
String tmpString="0"+i;
return tmpString;
}
else {
return String.valueOf(i);
}
} }

2.2 reduce side join

reduce side join是一种最简单的join方式, 之所以存在reduce side join,是因为在map阶段不能获取所有需要的join字段,即:同一个key对应的字段可能位于不同map中。Reduce   side join是非常低效的,因为shuffle阶段要进行大量的数据传输。

假设要进行join的数据分别来自File1和File2.

在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag),比如:tag=0表示来自文件File1,tag=2表示来自文件File2。即:map阶段的主要任务是对不同文件中的数据打标签。

在reduce阶段,reduce函数获取key相同的来自File1和File2文件的value list,   然后对于同一个key,对File1和File2中的数据进行join(笛卡尔乘积)。即:reduce阶段进行实际的连接操作。

代码如下(需要再次修改):

package com.unionpayadvisors;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
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.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.hsqldb.lib.StringUtil; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class AccountTableJoin extends Configured implements Tool {
private static WriteLog log = WriteLog.getInstance(); private static String parseRaw(String str) {
if (StringUtil.isEmpty(str)) {
return str;
}
str = str.trim();
if (str.startsWith("\"")) {
str = str.substring(1);
}
if (str.endsWith("\"")) {
str = str.substring(0, str.length() - 1);
}
return str.trim();
} public static class MapClass extends
Mapper<LongWritable, Text, Text, NullWritable> {
// 用于缓存 user_account 中的数据 private Set<String> accountSet = new HashSet<String>(); private Text accKey = new Text();
private NullWritable nullValue = NullWritable.get();
private String[] kv; private Jedis jedis = new JedisPool("192.168.2.101", 6379).getResource();
// 此方法会在map方法执行之前执行
// @Override
protected void setup(Context context) throws
IOException,InterruptedException {
BufferedReader in = null;
try {
// 从当前作业中获取要缓存的文件 Path[] paths = DistributedCache.getLocalCacheFiles(context .getConfiguration()); String accountLine = null; for (Path path : paths) { if (path.toString().contains("account")) { in = new BufferedReader(new FileReader(path.toString())); while (null != (accountLine = in.readLine())) {
log.logger("AccountTableJoin",
"accountSet="+parseRaw(accountLine.split(",", -1)[0]));
accountSet.add(parseRaw(accountLine.split(",", -1)[0])); } } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { kv = value.toString().split(","); //map join: 在map阶段过滤掉不需要的数据
if(kv.length==4&&accountSet.contains(parseRaw(kv[0]))){
accKey.set(value);
context.write(accKey, nullValue);
} // log.logger("XXXXXXXXX", "length!" + kv.length);
// if (kv.length == 53) {
// context.write(new Text(String.valueOf(parseRaw(kv[53 - 1])+":"+jedis.exists(parseRaw(kv[53 - 1])))), nullValue);
//
// log.logger("XXXXXXXXX", "Jedis!" + parseRaw(kv[53 - 1]));
// if (jedis.exists(parseRaw(kv[53 - 1]))) {
//
// log.logger("XXXXXXXXX", "jedis.exists"
// + jedis.exists(parseRaw(kv[53 - 1])));
// accKey.set(value);
// context.write(accKey, nullValue);
// }
}
} public int run(String[] args) throws Exception { log.logger("XXXXXXXXX", "begin in"); Job job = new Job(getConf(), "AccountTableJoin"); job.setJobName("AccountTableJoin"); job.setJarByClass(AccountTableJoin.class); job.setMapperClass(MapClass.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
String[] otherArgs = new GenericOptionsParser(job.getConfiguration(),
args).getRemainingArgs(); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception {
log.logger("XXXXXXXXX", "begin connection Jedis!"); // jedis=jedisPool.getResource();
// log.logger("XXXXXXXXX", "find \"7a5abdf04ce2350424907bf234d8ac80\""
// + jedis.get("7a5abdf04ce2350424907bf234d8ac80"));
// log.logger("XXXXXXXXX", "exsit \"7a5abdf04ce2350424907bf234d8ac80\""
// + jedis.exists("7a5abdf04ce2350424907bf234d8ac80"));
int res = ToolRunner.run(new Configuration(), new AccountTableJoin(),
args);
// jedisPool.returnResource(jedis);
// jedisPool.destroy();
log.logger("XXXXXXXXX", "connection Jedis end!"); System.exit(res); } /*
* hadoop jar AccountTableJoin.jar AccountTableJoin
* /user/he/sample_account.del /user/he/SAMPLE_SUM_2012070809101112.del
* /user/he/ACCOUNT_JOIN_RESULT
*/
}

2.3 SemiJoin

SemiJoin,也叫半连接,是从分布式数据库中借鉴过来的方法。它的产生动机是:对于reduce side   join,跨机器的数据传输量非常大,这成了join操作的一个瓶颈,如果能够在map端过滤掉不会参加join操作的数据,则可以大大节省网络IO。

实现方法很简单:选取一个小表,假设是File1,将其参与join的key抽取出来,保存到文件File3中,File3文件一般很小,可以放到内存中。在map阶段,使用DistributedCache将File3复制到各个TaskTracker上,然后将File2中不在File3中的key对应的记录过滤掉,剩下的reduce阶段的工作与reducee   side join相同。

2.4 reduce side join + BloomFilter

在某些情况下,SemiJoin抽取出来的小表的key集合在内存中仍然存放不下,这时候可以使用BloomFiler以节省空间。

BloomFilter最常见的作用是:判断某个元素是否在一个集合里面。它最重要的两个方法是:add()   和contains()。最大的特点是不会存在false   negative,即:如果contains()返回false,则该元素一定不在集合中,但会存在一定的true   negative,即:如果contains()返回true,则该元素可能在集合中。

因而可将小表中的key保存到BloomFilter中,在map阶段过滤大表,可能有一些不在小表中的记录没有过滤掉(但是在小表中的记录一定不会过滤掉),这没关系,只不过增加了少量的网络IO而已。

Hadoop面试的时候也会问到 Hadoop上Join的实现,几乎是一道必问的问题,而极个别公司还会涉及到DistributedCache原理以及怎样利用DistributedCache进行Join操作。

hadoop 多表join:Map side join及Reduce side join范例的更多相关文章

  1. hadoop的压缩解压缩,reduce端join,map端join

    hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别 ...

  2. Map Reduce Application(Join)

    We are going to explain how join works in MR , we will focus on reduce side join and map side join. ...

  3. EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...

  4. Hadoop 多表关联

    一.实例描述 多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息.下面进入这个实例. 输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址列,包含地 ...

  5. Hadoop 单表关联

    前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础.单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘.下面进入这个实例. 1.实例描述 实例中给出chi ...

  6. Reduce Side Join实现

    关于reduce边join,其最重要的是使用MultipleInputs.addInputPath这个api对不同的表使用不同的Map,然后在每个Map里做一下该表的标识,最后到了Reduce端再根据 ...

  7. MapReduce的Reduce side Join

    1. 简单介绍 reduce side  join是全部join中用时最长的一种join,可是这样的方法可以适用内连接.left外连接.right外连接.full外连接和反连接等全部的join方式.r ...

  8. 【数据库】left join(左关联)、right join(右关联)、inner join(自关联)的区别

    left join(左关联).right join(右关联).inner join(自关联)的区别 用一张图说明三者的区别: 总结: left join(左联接) 返回包括左表中的所有记录和右表中关联 ...

  9. oracle 内连接(inner join)、外连接(outer join)、全连接(full join)

    转自:https://premier9527.iteye.com/blog/1659689 建表语句: create table EMPLOYEE(EID NUMBER,DEPTID NUMBER,E ...

随机推荐

  1. 李洪强iOS经典面试题143-绘图与动画

    李洪强iOS经典面试题143-绘图与动画   绘图与动画 CAAnimation的层级结构 CAPropertyAnimation是CAAnimation的子类,也是个抽象类,要想创建动画对象,应该使 ...

  2. ApplicationContext更强的功能-学习笔记

    ---恢复内容开始--- 一.国际化支持 二.资源访问 三.事件传递 国际化支持 ApplicationContext继承了org.springframework.context.MessageRes ...

  3. android获取本地图片并显示图片

    import java.io.FileNotFoundException; import android.content.ContentResolver; import android.content ...

  4. Oracel EBS - Search Report by Response & Group

  5. 《Linux内核设计与实现》读书笔记 第一、二章

    第一章    Linux内核简介 1.1Unix历史 Unix特点:1.很简洁 2.所有东西都被当成文件对待 3.Unix内核和相关的系统工具软件都是用C语言编写而成 4.进程创建非常迅速 所以Uni ...

  6. 简单的行为控制管理方法,自动登录,session定时验证码过期

    代码很简单 实现的方式很多,用cookies 用static 变量 file文件缓存 等等 比如 //简单行为管理,如果请求此方法次数多于5次,就显示验证码 吧当前方法的name传进来,有效时间是5分 ...

  7. Apache Flink初接触

    Apache Flink闻名已久,一直没有亲自尝试一把,这两天看了文档,发现在real-time streaming方面,Flink提供了更多高阶的实用函数. 用Apache Flink实现WordC ...

  8. source insight 编程风格(持续更新)

    1.字体Source Code Pro 出身于豪门Adobe,从名字上来看就知道是转为编码而生的.基本上也是拥有前面所提的编程字体的所有要素的.这个字体基本上具有编程字体所需的所有要素:等宽.支持Cl ...

  9. 用onerror处理图片获取失败问题

    <script> function errorImg(){ //当前事件的源 var obj=event.srcElement; //需要修改的图片的url obj.src="h ...

  10. 利用nodeJS实现的网络小爬虫

    var http=require("http");var cheerio=require('cheerio');var url="http://www.imooc.com ...