MapReduce 练习二 找朋友

/***
 * 思路:
 *   前两个字母顺序为键
 *   分组后,求交集。即可取结果
 */
    public static class FriendsMapper extends Mapper<LongWritable,Text,Text,Text>{

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //转为数组 user:firends
            String[] split = value.toString().split(":");
            //转为数组 firends
            String[] firends = split[1].split(",");

            //循环firends
            for (String firend : firends) {
                //user > firend A,B :split[1] firends
                /***
                 * A:41,B:42
                 *
                 *1、A,B : B,C,D,E,F,O
                 *   A,C : B,C,D,E,F,O
                 *   A,D : B,C,D,E,F,O
                 *   A,E : B,C,D,E,F,O
                 *   A,F : B,C,D,E,F,O
                 *   A,O : B,C,D,E,F,O
                 *
                 * B:A,C,E,K
                 *
                 *    ----------
                 *    A,B:A,C,E,K
                 *    B,C:A,C,E,K
                 *    B,E:A,C,E,K
                 *    B,K:A,C,E,K
                 *
                 * C:F,A,D,I
                 *
                 * 3、C,F:F,A,D,I
                 *    A,C:F,A,D,I
                 *    C,D:F,A,D,I
                 *    D,I:F,A,D,I
                 *
                 * 对比 然后前两个小的为键 分组
                 *  例如:
                 *      A,B,C,D,E,F
                 *      B,A,C,D,E,F
                 *
                 *    分组:A,B:C,D,E,F
                 *
                 */
                if (split[0].compareTo(firend) > 0) {
                    context.write(new Text(firend +","+ split[0]), new Text(split[1]));
                } else {
                    context.write(new Text(split[0] +","+ firend), new Text(split[1]));
                }
            }
        }
    }

    public static class FriendsReduce extends Reducer<Text,Text,Text,Text>{

        /***
         * A:41,B:42
         *
         * @param key
         * @param values
         * @param context
         * @throws IOException
         * @throws InterruptedException
         *1、A,B : B,C,D,E,F,O
         *   A,C : B,C,D,E,F,O
         *   A,D : B,C,D,E,F,O
         *   A,E : B,C,D,E,F,O
         *   A,F : B,C,D,E,F,O
         *   A,O : B,C,D,E,F,O
         */
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> ulist = new ArrayList<String>();
            List<String> flist = new ArrayList<String>();

            int flag =0;
            //A,B : B,C,D,E,F,O
            for (Text text : values) {
                String[] split = text.toString().split(",");

                if(flag==0){
                    //ulist:B,C,D,E,F,O
                    for (String string : split) {
                        ulist.add(string);
                    }
                }else{
                    //flist:A,C,E,K
                    for (String string : split) {
                        flist.add(string);
                    }
                }
                flag++;
            }

            flist.retainAll(ulist);

            if(flist.size()>0&& flag >=2){
                context.write(key, new Text(StringUtils.join(flist, ",")));
            }
        }
    }

    public static void main(String [] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration conf=new Configuration();
        Job job= Job.getInstance(conf);
        job.setJarByClass(Friends.class);

        //mapper/reducer
        job.setMapperClass(FriendsMapper.class);
        job.setReducerClass(FriendsReduce.class);

        //mapper
       job.setMapOutputKeyClass(Text.class);
       job.setMapOutputValueClass(Text.class);

       //reduce
       job.setOutputKeyClass(Text.class);
       job.setOutputValueClass(Text.class);

       //指定位置
       FileInputFormat.setInputPaths(job,new Path(args[0]));
       FileOutputFormat.setOutputPath(job,new Path(args[1]));

       boolean res = job.waitForCompletion(true);
       System.exit(res?0:1);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值