MapReduce求两个人的共同好友算法

MapReduce求两个人的共同好友算法

以下是qq的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的)

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

求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?

思路

需求中给出的传入数据格式为:

用户:该用户拥有的好友们

user1:frined1,friend2,friend3…… 

user2:frined1,friend2,friend3…… 

user3:frined1,friend2,friend3…… 

user4:frined1,friend2,friend3…… 

……

要求传出的格式为:

两个用户:两个用户的共同好友

user1-user2:friend1,friend2,friend3…… 

user1-user3:friend1,friend2,friend3…… 

user1-user4:friend1,friend2,friend3…… 

user2-user3:friend1,friend2,friend3…… 

user2-user4:friend1,friend2,friend3…… 

user3-user4:friend1,friend2,friend3…… 

……

我们可以先将传入数据格式转换成:

所有用户的好友们:拥有该好友的用户

friend1:user1,user2,user3,user4…… 

friend2:user1,user2,user3,user4…… 

friend3:user1,user2,user3,user4…… 
……

再转换成:

两个用户:两个用户的共同好友

user1-user2:friend1,friend2,friend3…… 

user1-user3:friend1,friend2,friend3…… 

user1-user4:friend1,friend2,friend3…… 

user2-user3:friend1,friend2,friend3…… 

user2-user4:friend1,friend2,friend3…… 

user3-user4:friend1,friend2,friend3…… 

1 CommomFriendsStepOneMapper

package cn.itcast.friends;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

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

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

        for (String friend : friends) {
            context.write(new Text(friend),new Text(user));
        }
    }
}

2 CommonFriendsStepOneReducer

package cn.itcast.friends;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CommonFriendsStepOneReducer extends Reducer<Text,Text,Text,Text> {
    @Override
    protected void reduce(Text friend, Iterable<Text> userLists, Context context) throws IOException, InterruptedException {
        StringBuffer userBuffer = new StringBuffer();
        for (Text user : userLists) {
            userBuffer.append(user).append(",");
        }
        context.write(friend,new Text(userBuffer.toString()));

    }
}

3 StepOneClient

package cn.itcast.friends;

import cn.itcast.mr.wordcount.WordCountMapper;
import cn.itcast.mr.wordcount.WordCountReducer;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class StepOneClient {
    public static void main(String[] args) throws Exception{
        //配置文件对象
        Configuration conf = new Configuration();
        //创建job对象实例 用于属性封装 任务的提交
        Job job = Job.getInstance(conf, "WordCount");

        //指定本次mr程序运行的主类
        job.setJarByClass(StepOneClient.class);

        //指定本次mr运行的mapper类 reducer类
        job.setMapperClass(CommomFriendsStepOneMapper.class);
        job.setReducerClass(CommonFriendsStepOneReducer.class);

        //指定mapper阶段输出的key value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //指定reducer阶段输出的key value的数据类型  也就是整个MapReduce程序最终输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        //指定本次mr程序输入 输出的数据路径
        FileInputFormat.setInputPaths(job,new Path("D:\\datasets\\friends\\input"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\datasets\\friends\\output"));

        //todo 提交job  submit()提交之后 客户端就和执行的程序断开了链接 无法实时获取程序执行的情况
//        job.submit();
        boolean result = job.waitForCompletion(true);// true表示开启监控并打印程序执行的信息
        //根据返回的结果 决定程序如何退出  如果执行程序 正常状态码0退出  否则1异常退出。
        System.exit(result ? 0 :1);
    }
}

4 StepOne结果

A	I,K,C,B,G,F,H,O,D,
B	A,F,J,E,
C	A,E,B,H,F,G,K,
D	G,C,K,A,L,F,E,H,
E	G,M,L,H,A,F,B,D,
F	L,M,D,C,G,A,
G	M,
H	O,
I	O,C,
J	O,
K	B,
L	D,E,
M	E,F,
O	A,H,I,J,F,

5 CommomFriendsStepTwoMapper

package cn.itcast.friends;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.omg.CORBA.FREE_MEM;

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

public class CommomFriendsStepTwoMapper extends Mapper<LongWritable, Text,Text,Text> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        final String[] friendAndUsers = value.toString().split("\t");
        String friend= friendAndUsers[0];
        final String[] users = friendAndUsers[1].split(",");
        Arrays.sort(users);
        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));
            }

        }

    }
}

6 CommonFriendsStepTwoReducer

package cn.itcast.friends;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CommonFriendsStepTwoReducer extends Reducer<Text,Text,Text,Text> {
    @Override
    protected void reduce(Text user_user, Iterable<Text> friends, Context context) throws IOException, InterruptedException {
        StringBuffer friendsList = new StringBuffer();
        for (Text friend : friends) {
           friendsList.append(friend).append(" ");
        }
        context.write(user_user,new Text(friendsList.toString()));
    }
}

7 SteptwoClient

package cn.itcast.friends;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class SteptwoClient {
    public static void main(String[] args) throws Exception{
        //配置文件对象
        Configuration conf = new Configuration();
        //创建job对象实例 用于属性封装 任务的提交
        Job job = Job.getInstance(conf, "WordCount");

        //指定本次mr程序运行的主类
        job.setJarByClass(SteptwoClient.class);

        //指定本次mr运行的mapper类 reducer类
        job.setMapperClass(CommomFriendsStepTwoMapper.class);
        job.setReducerClass(CommonFriendsStepTwoReducer.class);

        //指定mapper阶段输出的key value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //指定reducer阶段输出的key value的数据类型  也就是整个MapReduce程序最终输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        //指定本次mr程序输入 输出的数据路径
        FileInputFormat.setInputPaths(job,new Path("D:\\datasets\\friends\\output"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\datasets\\friends\\output2"));

        //todo 提交job  submit()提交之后 客户端就和执行的程序断开了链接 无法实时获取程序执行的情况
//        job.submit();
        boolean result = job.waitForCompletion(true);// true表示开启监控并打印程序执行的信息
        //根据返回的结果 决定程序如何退出  如果执行程序 正常状态码0退出  否则1异常退出。
        System.exit(result ? 0 :1);
    }
}

8 StepTwo结果

A-B	C E 
A-C	F D 
A-D	E F 
A-E	B C D 
A-F	C D B E O 
A-G	D E F C 
A-H	E O C D 
A-I	O 
A-K	D 
A-L	F E 
B-C	A 
B-D	E A 
B-E	C 
B-F	E A C 
B-G	C E A 
B-H	E C A 
B-I	A 
B-K	A 
B-L	E 
C-D	F A 
C-E	D 
C-F	D A 
C-G	F A D 
C-H	A D 
C-I	A 
C-K	D A 
C-L	F 
D-F	E A 
D-G	A E F 
D-H	A E 
D-I	A 
D-K	A 
D-L	F E 
E-F	C D B 
E-G	D C 
E-H	D C 
E-K	D 
F-G	C E D A 
F-H	C A D E O 
F-I	A O 
F-K	D A 
F-L	E 
G-H	D E C A 
G-I	A 
G-K	A D 
G-L	F E 
H-I	A O 
H-K	A D 
H-L	E 
I-K	A 

你可能感兴趣的:(MapReduce)