单表关联

实例描述:

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

    样例输入如下所示。

 

 

file:                         

 

child        parent            

Tom        Lucy

Tom        Jack

Jone        Lucy

Jone        Jack

Lucy        Mary

Lucy        Ben

Jack        Alice

Jack        Jesse

Terry        Alice

Terry        Jesse

Philip        Terry

Philip        Alma

Mark        Terry

Mark        Alma

 

 样例 输出如下所示。

    file:

 

grandchild        grandparent

Tom              Alice

Tom              Jesse

Jone              Alice

Jone              Jesse

Tom              Mary

Tom              Ben

Jone              Mary

Jone              Ben

Philip              Alice

Philip              Jesse

Mark              Alice

Mark              Jesse

 

 

本人思路:又当child又当parent的就是中间数据,该数据对应的child就是该数据所对应parent的grandChild;

              很明显的自连接,左表为 child:[parent1,parent2],右表为parent:[child1,child2]。这样一来,如果child和parent相同,他们就会被分为一组,根据上面的理论,该值就是中间数据!

              最后取出坐标的parent为grandParent,右表的child为greandChild,不过这里有个问题,他们合为一组了,怎么判断哪个是parent哪个是child?这里就应该在value值里加一个标识!

 

   代码如下:

              

 1 public static class Map extends Mapper<LongWritable, Text, Text,Text> {
 2         
 3         @Override
 4         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
 5             String childName    = "";//孩子名称
 6             String parentName   = "";//父母名称
 7             String relationType = "";//左右表标识
 8             
 9             //输入的一行预处理文本
10             StringTokenizer itr = new StringTokenizer(value.toString());
11             String[] values = new String[2];
12             int i = 0;
13             while (itr.hasMoreElements()) {
14                 values[i] = itr.nextToken();
15                 i++;
16             }
17             
18             if (values[0].compareTo("child")!=0) {
19                 childName    = values[0];
20                 parentName   = values[1];
21                 relationType = "1";//左表
22                 context.write(new Text(values[1]), new Text(String.format("%s+%s+%s",relationType,childName,parentName)));
23                 relationType = "2";//左表
24                 context.write(new Text(values[0]), new Text(String.format("%s+%s+%s",relationType,childName,parentName)));
25                 
26             }
27             
28             
29         }
30     }
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 public static class IntSumReducer extends Reducer<Text, Text, Text, Text> {
43         @Override
44         public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
45             
46             
47             List<String> childs  = new ArrayList<String>();
48             List<String> parents = new ArrayList<String>();
49             
50             for (Text value : values) {
51                 String[] split = value.toString().split("\\+");
52                 if (split[0].equals("1")) {
53                     childs.add(split[1]);
54                 }else{
55                     parents.add(split[2]);
56                 }
57             }
58             
59             for (String cds : childs) {
60                 for (String pts : parents) {
61                     context.write(new Text(cds), new Text(pts));
62                 }
63             }
64             
65         }
66     }

 

 

 

 

 

转载于:https://www.cnblogs.com/master-L/p/4386998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值