您的位置:首页 > 其它

MapReduce中的两表join几种方案简介

2015-09-18 11:48 513 查看

http://blog.csdn.net/leoleocmm/article/details/8602081

MapReduce中的两表join几种方案简介

5人收藏此文章,我要收藏发表于2个月前(2012-12-11
20:11) ,已有91次阅读 ,共0个评论

1. 概述

在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的。而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的技巧。

本文首先介绍了Hadoop上通常的JOIN实现方法,然后给出了几种针对不同输入数据集的优化方法。

2. 常见的join方法介绍

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

2.1 reduce side join

reduce side join是一种最简单的join方式,其主要思想如下:

在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阶段进行实际的连接操作。

2.2 map side join

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

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读取相应的文件。

2.3 SemiJoin

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

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

更多关于半连接的介绍,可参考:半连接介绍:http://wenku.baidu.com/view/ae7442db7f1922791688e877.html

2.4 reduce side join + BloomFilter

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

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

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

更多关于BloomFilter的介绍,可参考:http://blog.csdn.net/jiaomeng/article/details/1495500

3. 二次排序

在Hadoop中,默认情况下是按照key进行排序,如果要按照value进行排序怎么办?即:对于同一个key,reduce函数接收到的value list是按照value排序的。这种应用需求在join操作中很常见,比如,希望相同的key中,小表对应的value排在前面。

有两种方法进行二次排序,分别为:buffer and in memory sort和 value-to-key conversion。

对于buffer and in memory sort,主要思想是:在reduce()函数中,将某个key对应的所有value保存下来,然后进行排序。 这种方法最大的缺点是:可能会造成out of memory。

对于value-to-key conversion,主要思想是:将key和部分value拼接成一个组合key(实现WritableComparable接口或者调用setSortComparatorClass函数),这样reduce获取的结果便是先按key排序,后按value排序的结果,需要注意的是,用户需要自己实现Paritioner,以便只按照key进行数据划分。Hadoop显式的支持二次排序,在Configuration类中有个setGroupingComparatorClass()方法,可用于设置排序group的key值,具体参考:http://www.cnblogs.com/xuxm2007/archive/2011/09/03/2165805.html

4. 后记

最近一直在找工作,由于简历上写了熟悉Hadoop,所以几乎每个面试官都会问一些Hadoop相关的东西,而 Hadoop上Join的实现就成了一道必问的问题,而极个别公司还会涉及到DistributedCache原理以及怎样利用DistributedCache进行Join操作。为了更好地应对这些面试官,特整理此文章。

5. 参考资料

(1) 书籍《Data-Intensive Text Processing with MapReduce》 page 60~67 Jimmy Lin and Chris Dyer,University of Maryland,College Park

(2) 书籍《Hadoop In Action》page 107~131

(3) mapreduce的二次排序 SecondarySort:http://www.cnblogs.com/xuxm2007/archive/2011/09/03/2165805.html

(4) 半连接介绍:http://wenku.baidu.com/view/ae7442db7f1922791688e877.html

(5) BloomFilter介绍:http://blog.csdn.net/jiaomeng/article/details/1495500

(6)本文来自:http://dongxicheng.org/mapreduce/hadoop-join-two-tables/

————————————————————————————————————————————————

看完了上面的 hadoop 中 MR 常规 join 思路,下面我们来看一种比较极端的例子,大表 join 小表,而小表的大小在10M以下的情况:

之所以我这里说小表要限制10M以下,是因为我这里用到的思路是 :

file-》jar-》main String configuration -》configuration map
HashMap

步骤:

1、从jar里面读取的文件内容以String的形式存在main方法的 configuration context
全局环境变量里

2、在map函数里读取 context 环境变量的字符串,然后split字符串组建小表成为一个HashMap

这样一个大表关联小表的例子就ok了,由于context是放在namenode上的,而namenode对内存是有限制的,

所以你的小表文件不要太大,这样我们可以比较的方便的利用 context 做join了。

这种方式其实就是 2.2
map side join 的一种具体实现而已。

Talk is cheap,show you the code~

001
public
class
Test
{
002
003
public
static
class
MapperClass
extends
004
Mapper<LongWritable,
Text,Text,Text> {
005
006
Configuration
config =
null
;
007
HashSet<String>
idSet =
new
HashSet<String>();
008
HashMap<String,
String> cityIdNameMap =
new
HashMap<String,
String>();
009
Map<String,
String> houseTypeMap =
new
HashMap<String,
String>();
010
011
public
void
setup(Context
context) {
012
config
= context.getConfiguration();
013
if
(config
==
null
)
014
return
;
015
String
idStr = config.get(
"idStr"
);
016
String[]
idArr = idStr.split(
","
);
017
for
(String
id :idArr) {
018
idSet.add(id);
019
}
020
021
String
cityIdNameStr = config.get(
"cityIdNameStr"
);
022
String[]
cityIdNameArr = cityIdNameStr.split(
","
);
023
for
(String
cityIdName :cityIdNameArr) {
024
cityIdNameMap.put(cityIdName.split(
"\t"
)[
0
],
025
cityIdName.split(
"\t"
)[
1
]);
026
}
027
028
houseTypeMap.put(
"8"
,
"Test"
);
029
030
}
031
032
public
void
map(LongWritable
key,Text value,Context context)
033
throws
IOException,
InterruptedException {
034
035
String[]
info = value.toString().split(
"\\|"
);
036
String
insertDate = info[InfoField.InsertDate].split(
"
"
)[
0
]
037
.split(
"-"
)[
0
];
//
date:2012-10-01
038
insertDate
= insertDate
039
+
info[InfoField.InsertDate].split(
"
"
)[
0
].split(
"-"
)[
1
];
//
date:201210
040
041
String
userID = info[InfoField.UserID];
//
userid
042
if
(!idSet.contains(userID))
{
043
return
;
044
}
045
046
String
disLocalID =
""
;
047
String[]
disLocalIDArr = info[InfoField.DisLocalID].split(
","
);
048
if
(disLocalIDArr.length
>=
2
)
{
049
disLocalID
= disLocalIDArr[
1
];
050
}
else
{
051
try
{
052
disLocalID
= disLocalIDArr[
0
];
053
}
catch
(Exception
e) {
054
e.printStackTrace();
055
return
;
056
}
057
}
058
String
localValue = cityIdNameMap.get(disLocalID);
059
disLocalID
= localValue ==
null
?
disLocalID :localValue;
//
city
060
061
String[]
cateIdArr = info[InfoField.CateID].split(
","
);
062
String
cateId =
""
;
063
String
secondType =
""
;
064
if
(cateIdArr.length
>=
3
)
{
065
cateId
= cateIdArr[
2
];
066
if
(houseTypeMap.get(cateId)
!=
null
)
{
067
secondType
= houseTypeMap.get(cateId);
//
secondType
068
}
else
{
069
return
;
070
}
071
}
else
{
072
return
;
073
}
074
075
String
upType = info[InfoField.UpType];
076
String
outKey = insertDate +
"_"
+
userID +
"_"
+
disLocalID +
"_"
077
+
secondType;
078
String
outValue = upType.equals(
"0"
)
?
"1_1"
:
"1_0"
;
079
context.write(
new
Text(outKey),
new
Text(outValue));
080
}
081
}
082
083
public
static
class
ReducerClass
extends
084
Reducer<Text,
Text,NullWritable,Text> {
085
086
public
void
reduce(Text
key,Iterable<Text> values,Context context)
087
throws
IOException,
InterruptedException {
088
int
pv
=
0
;
089
int
uv
=
0
;
090
091
for
(Text
val :values) {
092
String[]
tmpArr = val.toString().split(
"_"
);
093
pv
+= Integer.parseInt(tmpArr[
0
]);
094
uv
+= Integer.parseInt(tmpArr[
1
]);
095
}
096
097
String
outValue = key +
"_"
+
pv +
"_"
+
uv;
098
context.write(NullWritable.get(),
new
Text(outValue));
099
100
}
101
}
102
103
public
String
getResource(String fileFullName)
throws
IOException
{
104
//
返回读取指定资源的输入流
105
InputStream
is =
this
.getClass().getResourceAsStream(fileFullName);
106
BufferedReader
br =
new
BufferedReader(
new
InputStreamReader(is));
107
String
s =
""
;
108
String
res =
""
;
109
while
((s
= br.readLine()) !=
null
)
110
res
= res.equals(
""
)
?s :res + 
","
+
s;
111
return
res;
112
}
113
114
public
static
void
main(String[]
args)
throws
IOException,
115
InterruptedException,
ClassNotFoundException {
116
Configuration
conf =
new
Configuration();
117
String[]
otherArgs =
new
GenericOptionsParser(conf,
args)
118
.getRemainingArgs();
119
if
(otherArgs.length
!=
2
)
{
120
System.exit(
2
);
121
}
122
123
String
idStr =
new
Test().getResource(
"userIDList.txt"
);
124
String
cityIdNameStr =
new
Test().getResource(
"cityIdName.txt"
);
125
conf.set(
"idStr"
,
idStr);
126
conf.set(
"cityIdNameStr"
,
cityIdNameStr);
127
Job
job =
new
Job(conf,
"test01"
);
128
//
job.setInputFormatClass(TextInputFormat.class);
129
job.setJarByClass(Test.
class
);
130
job.setMapperClass(Test.MapperClass.
class
);
131
job.setReducerClass(Test.ReducerClass.
class
);
132
job.setNumReduceTasks(
25
);
133
job.setOutputKeyClass(Text.
class
);
134
job.setOutputValueClass(Text.
class
);
135
job.setMapOutputKeyClass(Text.
class
);
136
job.setMapOutputValueClass(Text.
class
);
137
138
FileInputFormat.addInputPath(job,
new
Path(otherArgs[
0
]));
139
org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.setOutputPath(
140
job,
new
Path(otherArgs[
1
]));
141
142
System.exit(job.waitForCompletion(
true
)
?
0
:
1
);
143
}
144
}
说明:

1、getResource()
方法指定了可以从jar包中读取配置文件,并拼接成一个String返回。

2、setup()
方法起到一个mapreduce前的初始化的工作,他的作用是从 context 中

获取main中存入的配置文件字符串,并用来构建一个hashmap,放在map外面,

每个node上MR前只被执行一次。

推荐阅读:

使用HBase的MAP侧联接

http://blog.sina.com.cn/s/blog_ae33b83901016lkq.html

PS:关于如何从jar包中读取配置文件,请参考:

(1)深入jar包:从jar包中读取资源文件
http://www.iteye.com/topic/483115

(2)读取jar内资源文件
/article/3880082.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: