MapReduce简单实践:两步实现查找共同好友

问题需求:现在有某社交网络中的记录每个用户的好友的数据集,数据的具体格式如下所示,冒号前为用户的代号,冒号后面为该用户的好友的代号,好友之间以逗号分隔。现在需求是根据此数据集,求出任意两个人之间的共同好友都有谁(好友关系是单向的,也就是说A的好友里面有E,但是E的好友里面不一定有A)。例如可以很明显的看出 A,B用户的共同好友有:C和E;A,C用户的共同好友有:D和F;

原始数据:

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

使用MapReduce对这个问题进行处理的时候可以通过两个MapReduce任务完成这个需求:

1、对原始数据进行反转解析,找出都有谁的好友里面有该用户;例如对于原始 A:B,C,D,F,E,O 数据,通过在map task中解析成 < B, A>< C, A>< D,A>< F,A>…的形式,这一系列的键值对表示所有代号为key值的用户,他的好友里面都有value值代表的用户。然后对这些键值对在reduce task中对key值相同的键值对(< A, B>< A, D>< A, F>…)进行拼接,拼接成< A B,D,F, …>的形式作为第一次MapReduce任务的输出。

这里写图片描述

上图为第一次MapReduce的输出,以其中的第一行数据(A F,D,O,I,H,B,K,G,C,)为例,这一行数据的含义是:F,D,O,I,H,B,K,G,C 这些用户的好友里面都有A。即分隔符后面的所有用户的好友里面都有分隔符前面的用户

2、根据第一次MapReduce的输出结果,我们可以很容易的想到,只要把分隔符后面的用户两两任意组合,就可以得到这两个用户的一个共同好友。以第一行数据为例,拆分之后可以得到:< F-D , A>, < F-O , A>, < F-I , A> … < D-O , A>,< D-I , A> …然后我们再在reduce task中按照相同的key值对键值对进行拼接,就得到了整个数据集中任意两个用户之间的共同好友的列表。

这里写图片描述

以下为两次MapReduce的实现代码

- Step 1:

package com;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

public class FindCommonFriendsStep1 {
    public static class FindCommonFriendsStep1Mapper extends Mapper<LongWritable, Text, Text, Text> {
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();     //获取Mapper端的输入值
            String[] userAndfriends = line.split(":");      
            String user = userAndfriends[0];        //获取输入值中的用户名称
            String[] friends = userAndfriends[1].split(",");        //获取输入值中的好友列表
            for (String friend : friends) {
                context.write(new Text(friend), new Text(user));        //以<B,A><C,A><D,A><F,A>...的格式输出
            }
        }
    }

    public static class FindCommonFriendsStep1Reducer extends Reducer<Text, Text, Text, Text> {
        public void reduce(Text friend, Iterable<Text> users, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer();
            for (Text user : users) {
                sb.append(user).append(",");        //对键值相同的键值对<A,B><A,D><A,F>...进行拼接
            }
            context.write(friend, new Text(sb.toString()));     //以<A   B,D,F, ...>的形式写入到HDFS上,作为中间结果,注意中间是以制表符(\t)分隔开
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();    //获取参数
        if (otherArgs.length < 2) {
            System.err.println("error");
            System.exit(2);    //如果获取到的参数少于两个,报错之后退出作业
        }
        Job job = Job.getInstance(conf, "find common friends step1");
        job.setJarByClass(FindCommonFriendsStep1.class);    //加载处理主类
        job.setMapperClass(FindCommonFriendsStep1Mapper.class);    //指定Mapper类
        job.setReducerClass(FindCommonFriendsStep1Reducer.class);    //指定Reducer类
        job.setOutputKeyClass(Text.class);    //指定Reducer输出键的类型
        job.setOutputValueClass(Text.class);    //指定Reducer输出值的类型
        for (int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

- Step 2:

package com;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;
import java.util.Arrays;

public class FindCommonFriendsStep2 {
    public static class FindCommonFriendsStep2Mapper extends Mapper<LongWritable, Text, Text, Text> {
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] friendAndusers = line.split("\t");
            String friend = friendAndusers[0];
            String[] users = friendAndusers[1].split(",");
            Arrays.sort(users);    // 下面的循环实现了对分隔符之后的任意两个用户的拼接,并作为新的key值生成键值对
            for (int i = 0; i < users.length - 2; i++) {    
                for (int j = i + 1; j < users.length - 1; j++) {
                    context.write(new Text("[" + users[i] + "-" + users[j] + "]:"), new Text(friend));
                }
            }
        }
    }

    public static class FindCommonFriendsStep2Reducer extends Reducer<Text, Text, Text, Text> {
        public void reduce(Text user, Iterable<Text> friends, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer();
            for (Text friend : friends) {
                sb.append(friend).append(",");    //对key值相同的键值对进行拼接
            }
            context.write(user, new Text(sb.toString()));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length < 2) {
            System.err.println("error");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "find common friends step2");
        job.setJarByClass(FindCommonFriendsStep2.class);
        job.setMapperClass(FindCommonFriendsStep2Mapper.class);
        job.setReducerClass(FindCommonFriendsStep2Reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        for (int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

猜你喜欢

转载自blog.csdn.net/khxu666/article/details/80403441
今日推荐