/***
* 思路:
* 前两个字母顺序为键
* 分组后,求交集。即可取结果
*/
public static class FriendsMapper extends Mapper<LongWritable,Text,Text,Text>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//转为数组 user:firends
String[] split = value.toString().split(":");
//转为数组 firends
String[] firends = split[1].split(",");
//循环firends
for (String firend : firends) {
//user > firend A,B :split[1] firends
/***
* A:41,B:42
*
*1、A,B : B,C,D,E,F,O
* A,C : B,C,D,E,F,O
* A,D : B,C,D,E,F,O
* A,E : B,C,D,E,F,O
* A,F : B,C,D,E,F,O
* A,O : B,C,D,E,F,O
*
* B:A,C,E,K
*
* ----------
* A,B:A,C,E,K
* B,C:A,C,E,K
* B,E:A,C,E,K
* B,K:A,C,E,K
*
* C:F,A,D,I
*
* 3、C,F:F,A,D,I
* A,C:F,A,D,I
* C,D:F,A,D,I
* D,I:F,A,D,I
*
* 对比 然后前两个小的为键 分组
* 例如:
* A,B,C,D,E,F
* B,A,C,D,E,F
*
* 分组:A,B:C,D,E,F
*
*/
if (split[0].compareTo(firend) > 0) {
context.write(new Text(firend +","+ split[0]), new Text(split[1]));
} else {
context.write(new Text(split[0] +","+ firend), new Text(split[1]));
}
}
}
}
public static class FriendsReduce extends Reducer<Text,Text,Text,Text>{
/***
* A:41,B:42
*
* @param key
* @param values
* @param context
* @throws IOException
* @throws InterruptedException
*1、A,B : B,C,D,E,F,O
* A,C : B,C,D,E,F,O
* A,D : B,C,D,E,F,O
* A,E : B,C,D,E,F,O
* A,F : B,C,D,E,F,O
* A,O : B,C,D,E,F,O
*/
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
List<String> ulist = new ArrayList<String>();
List<String> flist = new ArrayList<String>();
int flag =0;
//A,B : B,C,D,E,F,O
for (Text text : values) {
String[] split = text.toString().split(",");
if(flag==0){
//ulist:B,C,D,E,F,O
for (String string : split) {
ulist.add(string);
}
}else{
//flist:A,C,E,K
for (String string : split) {
flist.add(string);
}
}
flag++;
}
flist.retainAll(ulist);
if(flist.size()>0&& flag >=2){
context.write(key, new Text(StringUtils.join(flist, ",")));
}
}
}
public static void main(String [] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf=new Configuration();
Job job= Job.getInstance(conf);
job.setJarByClass(Friends.class);
//mapper/reducer
job.setMapperClass(FriendsMapper.class);
job.setReducerClass(FriendsReduce.class);
//mapper
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
//reduce
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//指定位置
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
boolean res = job.waitForCompletion(true);
System.exit(res?0:1);
}
MapReduce 练习二 找朋友
最新推荐文章于 2020-11-18 18:47:18 发布