MapReduce练习二(单表关联,多表关联,倒排索引)

这篇博客详细介绍了如何使用MapReduce进行单表关联、多表关联以及创建倒排索引的实践。通过MapReduce实现从child-parent表中输出grandchild-grandparent表,通过地址编号连接工厂表和地址表,以及构建单词在各文件中的倒排索引。文章提供了相关的Map和Reduce代码示例。
摘要由CSDN通过智能技术生成

相关链接:
MapReduce练习一(计数,去重,排序,平均成绩)
MapReduce练习二(单表关联,多表关联,倒排索引)
一些省略的代码可以在练习一中找到。

一,单表关联

要求:给出child-parent(孩子—父母)表,要求输出grandchild-grandparent(孙子—爷奶)表。

Map输出2个表,第一个表key为父母,设一个标识位tag=L,value为tag+孩子+父母,称为左表;第二个表key为孩子,tag=R,value为tag+孩子+父母,称为右表。其实就是左一个child-parent表,右边根据parent连接出一个child-parent表,只不过左表里的parent在右表中是child。
在shuffle时,key相同的会合并形成value-list,所以在reduce时,我们要的祖孙表是满足value-list中同时有L标识和R标识的,也就是根据parent连接成功的。
此时L表中的child放入grandchild数组,R表的parent放入grandparent数组,然后一内一外两个循环遍历这俩数组打印输出(这叫笛卡尔积?)。
给出map和reduce的代码:

public class STjoin extends Configured implements Tool{
   
    static int row = 0; // 标记表头行
    public static class Map extends Mapper<LongWritable, Text, Text, Text>{
   
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
            String line = value.toString();
            String[] words = line.split(" +");
            String childName = words[0];
            String parentName = words[1];
            String tag = new String(); // 左右表的标识
            // 略去表头那一行
            if(!childName.equals("child")){
                // 输出左表,以父母为key,想想为啥以父母为key作为左表,其实就是用父母连接两个表
                tag = "L";
                context.write(new Text(parentName), new Text(tag + "+" + childName + "+" + parentName));
                // 输出右表,以孩子为key
                tag = "R";
                context.write(new Text(childName), new Text(tag + "+" + childName + "+" + parentName));
            }
        }
    }
    public static class Reduce extends Reducer<Text, 
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值