单表关联

实例描述:

    实例中给出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     }

 

 

 

 

 

posted @ 2015-04-02 15:05  Mr._L  阅读(145)  评论(0编辑  收藏  举报