【图文解析 】MapJoin-DistributedCache 应用

1、MapReduce Join 介绍 
在各种实际业务场景中,按照某个关键字对两份数据进行连接是非常常见的。如果两份数据 都比较小,那么可以直接在内存中完成连接。如果是大数据量的呢?显然,在内存中进行连 接会发生 OOM。MapReduce 可以用来解决大数据量的链接 
 
MapReduce 的 Join 操作主要分两类:MapJoin 和 ReduceJoin 
 
先看 ReduceJoin: 

  • map 阶段,两份数据 data1 和 data2 会被 map 分别读入,解析成以链接字段为 key 以查 询字段为 value 的 key-value 对,并标明数据来源是 data1 还是 data2。
  • reduce 阶段,reducetask 会接收来自 data1 和 data2 的相同 key 的数据,在 reduce 端进 行乘积链接,最直接的影响是很消耗内存,导致 OOM 

 
再看 MapJoin: MapJoin 适用于有一份数据较小的连接情况。做法是直接把该小份数据直接全部加载到内存 当中,按链接关键字建立索引。然后大份数据就作为 MapTask 的输入,对 map()方法的每次 输入都去内存当中直接去匹配连接。然后把连接结果按 key 输出,这种方法要使用 hadoop 中的 DistributedCache 把小份数据分布到各个计算节点,每个 maptask 执行任务的节点都需 要加载该数据到内存,并且按连接关键字建立索引 

 

2、需求 
现有两份数据 movies.dat 和 ratings.dat 数据样式分别为:

Movies.dat 

字段含义:movieid, moviename, movietype 
 
Ratings.dat 

字段含义:userid, movieid, rate, timestamp  
Select * from movie a join ratings b on a.movieid = b.movieid 
 
现要求对两表进行连接,要求输出最终的结果有以上六个字段: movieid, userid, rate, moviename, movietype, timestamp 


3、实现 
第一步:封装 MovieRate,方便数据的排序和序列化 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释代码并讲解上下文关系import kmeans.utils.CentersOperation; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class KMeansMapper extends Mapper<LongWritable, Text, Text, Text> { private List<List<Double>> centers = new ArrayList<>(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] dimensions; List<Double> point = new ArrayList<>(); double centerIndex = 1; double minDistance = Double.MAX_VALUE; int iteration = context.getConfiguration().getInt(KMeans.ITERATION, 0); if (centers.size() == 0) { String centersPath = context.getCacheFiles()[0].toString(); centers = CentersOperation.getCenters(centersPath, true); } dimensions = value.toString().split("[,\\t]"); for (int i = 0; i < dimensions.length - 1; i++) { point.add(Double.parseDouble(dimensions[i])); } for (int i = 0; i < centers.size(); i++) { double distance = 0; List<Double> center = centers.get(i); for (int j = 0; j < center.size(); j++) { distance += Math.pow((point.get(j) - center.get(j)), 2); } distance = Math.sqrt(distance); if (distance < minDistance) { minDistance = distance; centerIndex = i + 1; } } String pointData = value.toString().split("\t")[0]; if (iteration == (KMeans.MAX_ITERATION - 1)) { context.write(new Text(pointData), new Text(String.valueOf(centerIndex))); } else { context.write(new Text(String.valueOf(centerIndex)), new Text(pointData)); } } }
最新发布
05-28

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值