# 算法

假设有以下好友列表，A的好友有B,C,D,F,E,O;   B的好友有A,C,E,K 以此类推

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得到 某个用户是哪些用户的共同好友。
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);

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

}
运行的到的结果如下：

/**
* 遍历有同个好友的用户的列表进行组合，得到两人的共同好友
*/
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);

boolean completion = job.waitForCompletion(true);
System.out.println(completion);
}
}
得到的结果如下:

今天给大家分享的好友推荐算法就是这些，今天的只是一个小小的案例，现实场景中的运算肯定要比这个复杂的多，

posted @ 2019-10-09 19:01  喜讯XiCent  阅读(...)  评论(...编辑  收藏