以下是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
求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?
以A举例 A 的 好友有 B C D F E O
B ----------> A C E K AB互相为好友,共同好友是C和 E
第一步:
mapper
package com.wxj.togetherfriend.step1;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/*
* Created by wxj on 2019/8/19 0019 20:15
*/
public class Step1Mapper extends Mapper<LongWritable, Text,Text,Text> {
Text t = new Text();
Text t2 = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//根据分号把每行数据进行切分
String[] split = value.toString().split(":");
//把分号后的数据根据逗号进行切分
String[] sp = split[1].split(",");
//写出数据
t2.set(split[0]);
for (String s : sp) {
t.set(s);
context.write(t,t2);
}
}
}
reduce
package com.wxj.togetherfriend.step1;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/*
* Created by wxj on 2019/8/19 0019 23:03
*/
public class Step1Reducer extends Reducer<Text,Text,Text,Te