MapReduce之连接模式一:reduce端连接

INPUT:表a:userID,user.location;表b:userID,coments.votes
OUTPUT:userID,user.location,coments.votes

joinMapper:
    public static class joinAMapper extends Mapper<Object,Text,Text,Text>{  
            private Text outKey = new Text();
            private Text outValue = new Text();
            public void map(Object key,Text value,Context context){
                    StringTokenizer itr = new StringTokenizer(value.toString());
                    while(itr.hasMoreTokens()){
                    String[] info  = itr.splite(",");
                    outKey.set(info[0]);
                    outValue.set("A"+info[1]);
                    context.write(outKey,outValue);
                    }

            }
    }

    public static class joinBMapper extends Mapper<Object,Text,Text,Text>{  
            private Text outKey = new Text();
            private Text outValue = new Text();
            public void map(Object key,Text value,Context context){
                    StringTokenizer itr = new StringTokenizer(value.toString());
                    while(itr.hasMoreTokens()){
                    String[] info  = itr.splite(",");
                    outKey.set(info[0]);
                    outValue.set("B"+info[1]);
                    context.write(outKey,outValue);
                    }

            }
    }

joinReducer:

    public static class joinReducer extends Reducer<Text,Text,Text,Text>{
            private Text tmp = new Text();
            private ArrayList listA = new ArrayList();
            private ArrayList listB = new ArrayList();
            public void reduce(Text text,Iterable values,Context context){
                while(values.hasNext()){
                    tmp = values.next();
                    if(tmp.charAt(0)=='A'){
                        listA.add(tmp.subString(1));
                    }else if(tmp.charAt(0)=='B'){
                        listB.add(tmp.subString(1));
                    }

                }


                execute.JoinLogic(context);
            }

    }




你可能感兴趣的:(Java,Hadoop)