MR案例:Map-Join
适用场景:一张表十分小【key不可重复】、一张表非常大。
用法:在Job提交时,首先将小表加载到 DistributedCache 分布式缓存中,然后从DistributeCache中读取小表解析成 key/value 保存到内存中(可以放在Hash Map等容器中)。然后扫描大表中的每条记录的 key 是否能在内存中找到相同 join key 的记录,如果有则直接输出结果。
package join.map; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap; 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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* map-join中小表的数据如下:
*
* 1 Beijing
* 2 Guangzhou
* 3 Shenzhen
* 4 Xian
*
* 大表的数据如下:
*
* Beijing Red Star 1
* Shenzhen Thunder 3
* Guangzhou Honda 2
* Beijing Rising 1
* Guangzhou Development Bank 2
* Tencent 3
* Back of Beijing 1
*/
public class MapJoin { public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(MapJoin2.class);
//此方法已过时,被job.addCacheFile()所取代
//DistributedCache.addCacheFile(new URI("hdfs://10.16.17.182:9000/test/in/address.txt"), conf);
//加载小表到 分布式缓存DistributedCache
job.addCacheFile(new Path(args[0]).toUri());
job.setMapperClass(MJMapper.class);
job.setNumReduceTasks(0);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
System.exit(job.waitForCompletion(true)? 0:1);
} public static class MJMapper extends Mapper<LongWritable, Text, Text, Text>{ /**
* 此map是存放小表数据用的
* 注意小表的key是不能重复的,类似与数据库的外键表
* 在这里的小表,就相当于一个外键表
* **/
private HashMap<String, String> map=new HashMap<String, String>(); @Override
protected void setup(Context context) throws IOException, InterruptedException { BufferedReader br=null; // 读取文件流
String line; // 获取DistributedCached里面 的共享文件
Path[] paths = context.getLocalCacheFiles(); for(Path path : paths){
if(path.getName().indexOf("address") >= 0){ //如果是 address文件
br=new BufferedReader(new FileReader(path.toString())); while((line=br.readLine()) != null){ //读取文件中的每一行
String[] splited = line.split("\t"); map.put(splited[0], splited[1]); //将小表解析成 key/value 存放进map
}
}
}
} /**
* map阶段读取并处理大表中的数据
* 小表中的数据是加载到HashMap中的,无需从hdfs读取
*/
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { if(value==null || ("").equals(value.toString())){ //跳过空值
return;
} String[] splited = value.toString().split("\t");
if(map.get(splited[1]) != null){ //map中大表的 key 对应的 value 不为空
Text keyOut = new Text(splited[0]); //key=大表的第一列
Text valueOut = new Text(map.get(splited[1])); //value=小表的第二列
context.write(keyOut, valueOut);
}
}
}
}
MR案例:Map-Join的更多相关文章
- MR案例:Reduce-Join
问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...
- MR案例:倒排索引
1.map阶段:将单词和URI组成Key值(如“MapReduce :1.txt”),将词频作为value. 利用MR框架自带的Map端排序,将同一文档的相同单词的词频组成列表,传递给Combine过 ...
- MR案例:小文件处理方案
HDFS被设计来存储大文件,而有时候会有大量的小文件生成,造成NameNode资源的浪费,同时也影响MapReduce的处理效率.有哪些方案可以合并这些小文件,或者提高处理小文件的效率呢? 1). 所 ...
- Hive 的 map join
学习自 http://blog.csdn.net/xqy1522/article/details/6699740 1. Map Join 的使用场景: 关联操作中有一张表非常小 不等值的链接操作 2. ...
- HIVE: Map Join Vs Common Join, and SMB
HIVE Map Join is nothing but the extended version of Hash Join of SQL Server - just extending Hash ...
- 使用Spark进行搜狗日志分析实例——map join的使用
map join相对reduce join来说,可以减少在shuff阶段的网络传输,从而提高效率,所以大表与小表关联时,尽量将小表数据先用广播变量导入内存,后面各个executor都可以直接使用 pa ...
- MapReduce编程之Map Join多种应用场景与使用
Map Join 实现方式一:分布式缓存 ● 使用场景:一张表十分小.一张表很大. ● 用法: 在提交作业的时候先将小表文件放到该作业的DistributedCache中,然后从DistributeC ...
- MapReduce之Map Join
一 介绍 之所以存在Reduce Join,是因为在map阶段不能获取所有需要的join字段,即:同一个key对应的字段可能位于不同map中.Reduce side join是非常低效的,因为shuf ...
- MR案例:CombineFileInputFormat
CombineFileInputFormat是一个抽象类.Hadoop提供了两个实现类CombineTextInputFormat和CombineSequenceFileInputFormat. 此案 ...
- MR案例:倒排索引 && MultipleInputs
本案例采用 MultipleInputs类 实现多路径输入的倒排索引.解读:MR多路径输入 package test0820; import java.io.IOException; import j ...
随机推荐
- XML 配置里的 Bean 自动装配
在XML文件中,先看一下下面的代码: <bean id="student" class="com.jeremy.spring.beans.student" ...
- Elven Postman---hdu5444(二叉树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5444 有一个序列,由这个序列可以画出一颗二叉树(每个节点的左边(W)都比它大,右边(E)都比它小), ...
- bat脚本运行py文件失败(一闪而过)
简单记录下问题及原因,方便回顾. 问题 通过 bat 脚本运行 py 文件时,终端一闪而过,没能成功运行. 查证后发现问题出在编码上: 首先检查下bat文件编码格式(推荐 notepad++ ) 打开 ...
- 类的super
我们经常在类的继承当中使用super(), 来调用父类中的方法.例如下面: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A: def func(self): ...
- git提交到远程虚拟机
git到自己的虚拟机中第一步:打通git(一)Linux中(ip为10.1.8.1)1.安装git如:Ubuntu中安装gitapt install git 2.Ubuntu中添加git用户sudo ...
- UVA Team Queue
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013840081/article/details/26180081 题目例如以下: Team Qu ...
- Spring第三弹—–编码剖析Spring管理Bean的原理
先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- 基于Flume+Kafka+ Elasticsearch+Storm的海量日志实时分析平台(转)
0背景介绍 随着机器个数的增加.各种服务.各种组件的扩容.开发人员的递增,日志的运维问题是日渐尖锐.通常,日志都是存储在服务运行的本地机器上,使用脚本来管理,一般非压缩日志保留最近三天,压缩保留最近1 ...
- 二进制x&(x-1);
求下面函数的返回值(微软) int func(x) { int countx = 0; while(x) { countx ++; x ...
- jmeter使用代理服务器录制脚本端口号被占用
初学jmeter工具,在设置端口号时,使用8080,IE设置的局域网端口也为8080,启动代理服务器时,提示:Could not create script recorder-port in use. ...