MapReduce案例-好友推荐

用过各种社交平台(如QQ、微博、朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图)。它的算法主要是根据你们之间的共同好友数进行推荐,当然也有其他如爱好、特长等等。共同好友的数量越多,表明你们可能认识,系统便会自动推荐。今天我将向大家介绍如何使用MapReduce计算共同好友

image

算法

    假设有以下好友列表,A的好友有B,C,D,F,E,O;   B的好友有A,C,E,K 以此类推
    那我们要如何算出A-O用户每个用户之间的共同好友呢?
A:B,C,D,F,E,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:A,B,C,D,E,O,M
G:A,C,D,E,F
H:A,C,D,E,O
I:A,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J

    下面我们将演示分步计算,思路主要如下:先算出某个用户是哪些用户的共同好友,
    如A是B,C,D,E等的共同好友。遍历B,C,D,E两两配对如(B-C共同好友A,注意防止重复B-C,C-B)作为key放松给reduce端,
    这样reduce就会收到所有B-C的共同好友的集合。可能到这里你还不太清楚怎么回事,下面我给大家画一个图。

image

代码演示

由上可知,此次计算由两步组成,因此需要两个MapReduce程序先后执行

    第一步:通过mapreduce得到 某个用户是哪些用户的共同好友。
public class FriendsDemoStepOneDriver {

    static class FriendsDemoStepOneMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();

            String[] split = line.split(":");
            String user = split[0];
            String[] friends = split[1].split(",");

            for (String friend : friends) {
//                输出友人,人。 这样的就可以得到哪个人是哪些人的共同朋友
                context.write(new Text(friend),new Text(user));
            }
        }
    }

    static class FriendsDemoStepOneReducer extends Reducer<Text,Text,Text,Text>{

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuilder sb = new StringBuilder();
            for (Text person : values) {
                sb.append(person ",");
            }

            context.write(key,new Text(sb.toString()));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("mapreduce.framework.name","local");
        conf.set("fs.defaultFS","file:///");

        Job job = Job.getInstance(conf);

        job.setJarByClass(FriendsDemoStepOneDriver.class);

        job.setMapperClass(FriendsDemoStepOneMapper.class);
        job.setReducerClass(FriendsDemoStepOneReducer.class);

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

        FileInputFormat.setInputPaths(job,new Path("/Users/kris/Downloads/mapreduce/friends/friends.txt"));
        FileOutputFormat.setOutputPath(job,new Path("/Users/kris/Downloads/mapreduce/friends/output"));

        boolean completion = job.waitForCompletion(true);
        System.out.println(completion);
    }

}

运行的到的结果如下:image

由图可见:I,K,C,B,G,F,H,O,D都有好友A;A,F,J,E都有好友B。接下来我们只需组合这些拥有相同的好友的用户,
作为key发送给reduce,由reduce端聚合d得到所有共同的好友

/**
 * 遍历有同个好友的用户的列表进行组合,得到两人的共同好友
 */
public class FriendsDemoStepTwoDriver {

    static class FriendDemoStepTwoMapper extends Mapper<LongWritable, Text, Text, Text> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String line = value.toString();

            String[] split = line.split("\t");
            String friend = split[0];
            String[] persons = split[1].split(",");

            Arrays.sort(persons);

            for (int i = 0; i < persons.length-1; i  ) {
                for (int i1 = i 1; i1 < persons.length; i1  ) {
                    context.write(new Text(persons[i] "--" persons[i1]),new Text(friend));
                }
            }

        }
    }

    static class FriendDemoStepTwoReducer extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuilder sb = new StringBuilder();

            for (Text friend : values) {
                sb.append(friend   ",");
            }

            context.write(key,new Text(sb.toString()));
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("mapreduce.framework.name","local");
        conf.set("fs.defaultFS","file:///");

        Job job = Job.getInstance(conf);

        job.setJarByClass(FriendsDemoStepOneDriver.class);

        job.setMapperClass(FriendDemoStepTwoMapper.class);
        job.setReducerClass(FriendDemoStepTwoReducer.class);

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

        FileInputFormat.setInputPaths(job,new Path("/Users/kris/Downloads/mapreduce/friends/output"));
        FileOutputFormat.setOutputPath(job,new Path("/Users/kris/Downloads/mapreduce/friends/output2"));

        boolean completion = job.waitForCompletion(true);
        System.out.println(completion);
    }
}

得到的结果如下:image

如图,我们就得到了拥有共同好友的用户列表及其对应关系,在实际场景中再根据用户关系(如是否已经是好友)进行过滤,在前端展示,就形成了我们所看到"可能认识"或者"好友推荐"啦~

    今天给大家分享的好友推荐算法就是这些,今天的只是一个小小的案例,现实场景中的运算肯定要比这个复杂的多,
    但是思路和方向基本一致,如果有更好的建议或算法,欢迎与小吴一起讨论喔~
    如果您喜欢这篇文章的话记得like,share,comment喔(^^)
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种用于处理大规模数据集的编程模型和软件框架。它可以将大规模数据集分解成多个小的数据块,并在分布式计算集群上进行并行处理。二度好友推荐是一种基于社交网络的推荐算法,它可以根据用户的好友关系,推荐用户可能认识但尚未添加为好友的人。 在MapReduce实现二度好友推荐算法的一种常见方法是使用两个MapReduce作业。第一个作业用于计算用户的直接好友关系,第二个作业用于计算用户的二度好友关系。 以下是一个简单的示例,演示了如何使用MapReduce实现二度好友推荐算法: 1. 第一个作业(计算直接好友关系): ```python # Mapper函数 def mapper1(key, value): user, friends = value.split("\t") for friend in friends.split(","): yield (user, friend), 1 # Reducer函数 def reducer1(key, values): yield key, sum(values) # 执行第一个作业 input_data = # 输入数据,格式为:user1\tfriend1,friend2,friend3... output_data1 = map_reduce(input_data, mapper1, reducer1) ``` 2. 第二个作业(计算二度好友关系): ```python # Mapper函数 def mapper2(key, value): user1, user2 = key yield (user1, user2), value # Reducer函数 def reducer2(key, values): user1, user2 = key mutual_friends = [] for value in values: mutual_friends.append(value) yield (user1, user2), mutual_friends # 执行第二个作业 output_data2 = map_reduce(output_data1, mapper2, reducer2) ``` 请注意,上述示例中的`map_reduce`函数是一个抽象的函数,用于执行MapReduce作业。具体的实现细节可以根据使用的编程语言和框架进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值