map端join算法实现

本文详细介绍了Hadoop MapReduce环境下实现Map端Join的原理和步骤,包括在Mapper中加载小表数据,以及如何通过MapReduce作业执行Map端Join操作。此外,还涉及到了社交粉丝数据分析和倒排索引的建立过程。
摘要由CSDN通过智能技术生成

map端join算法实现

1、原理阐述
适用于关联表中有小表的情形;
可以将小表分发到所有的map节点,这样,map节点就可以在本地对自己所读到的大表数据进行join并输出最终结果,可以大大提高join操作的并发度,加快处理速度
2、实现示例
–先在mapper类中预先定义好小表,进行join
–引入实际场景中的解决方案:一次加载数据库或者用

第一步:定义mapJoin

public class JoinMap extends Mapper<LongWritable,Text,Text,Text> {
HashMap<String,String> b_tab = new HashMap<String, String>();
String line = null;
/*
map端的初始化方法当中获取缓存文件,一次性加载到map当中来
*/
@Override
public void setup(Context context) throws IOException, InterruptedException {
//这种方式获取所有的缓存文件
// URI[] cacheFiles1 = DistributedCache.getCacheFiles(context.getConfiguration());
Path[] localCacheFiles = DistributedCache.getLocalCacheFiles(context.getConfiguration());
URI[] cacheFiles = DistributedCache.getCacheFiles(context.getConfiguration());
FileSystem fileSystem = FileSystem.get(cacheFiles[0], context.getConfiguration());
FSDataInputStream open = fileSystem.open(new Path(cacheFiles[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open));
while ((line = bufferedReader.readLine())!=null){
String[] split = line.split(",");
b_tab.put(split[0],split[1]+"\t"+split[2]+"\t"+split[3]);
}
fileSystem.close();
IOUtils.closeStream(bufferedReader);
}

@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    //这里读的是这个map task所负责的那一个切片数据(在hdfs上)
    String[] fields = value.toString().split(",");
    String orderId = fields[0];
    String date = fields[1];
    String pdId = fields[2];
    String amount = fields[3];
    //获取map当中的商品详细信息
    String productInfo = b_tab.get(pdId);
    context.write(new Text(orderId), new Text(date + "\t" + productInfo+"\t"+amount));
}

}

第二步:定义程序运行main方法

public class MapSideJoin extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Co

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值