1. 需求:给出A-O个人中每个人的好友列表,求出哪些人两两之间有共同好友,以及他们的共同好友都有谁。
       注意:这些人好友都是单向的,可能A是B的好友,但是B不一定是A的好友,这种类似的微博的关注,
                  A关注B,但是B不一定关注了A。
  2. 原始文件如下:
      
  3. 要求输出的格式如下:
     
  4. 思路分析:
       ⑴我们从上面可以现在我们知道A-O每个人拥有哪些好友,但是我们现在是要找出两两之间的人有哪些共同好友。那么
           我们可以逆向思维,第一步找出哪些好友拥有A,哪些好友拥有B.....依次找出,结果如下:
           
       ⑵通过得出上面的数据后,我们可以对后面的好友进行排序,避免重复,将 “拥有这名朋友的所有人”进行两两配对,并将配对后的         字符串当做键,“朋友”当做值输出,即输出<人-人,共同朋友>
           
  5. 代码实现,通过两次job运算
     a:FriendMapper01

    package com.kgf.mapreduce.friend;import java.io.IOException;import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;public class FriendMapper01 extends Mapper<LongWritable, Text, Text, Text>{Text k  =new Text();Text v  =new Text();@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {//1:获取一行数据String line = value.toString();//2:对一行数据进行切割String[] fields = line.split(":");String person = fields[0];String[] friends = fields[1].split(",");for (String friend : friends) {k.set(friend);v.set(person);context.write(k, v);}}
    }
    

    b:FriendReducer

    package com.kgf.mapreduce.friend;import java.io.IOException;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;public class FriendReducer extends Reducer<Text, Text, Text, Text> {@Overrideprotected void reduce(Text key, Iterable<Text> values,Context context)throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();//1:获取哪些好友都有对应的人for (Text text : values) {sb.append(text.toString()+",");}sb.deleteCharAt(sb.length()-1);context.write(key, new Text(sb.toString()));}
    }
    

    c:FriendDriver01

    package com.kgf.mapreduce.friend;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 FriendDriver01 {public static void main(String[] args) throws Exception {//1:获取Job对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//2:设置jarjob.setJarByClass(FriendDriver01.class);//3:关联Mapper和reducerjob.setMapperClass(FriendMapper01.class);job.setReducerClass(FriendReducer.class);//4:设置mapper输出参数job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//5:设置最终输出job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//6:设置文件输入输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));//7:提交boolean result = job.waitForCompletion(true);System.exit(result?0:1);}}
    

    d:FriengMapper02

    package com.kgf.mapreduce.friend;import java.io.IOException;
    import java.util.Arrays;import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;public class FriengMapper02 extends Mapper<LongWritable, Text, Text, Text>{@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {//1:获取一行String line = value.toString();//2:切割数据String[] fileds = line.split("\t");String friend = fileds[0];String[] persons = fileds[1].split(",");Arrays.sort(persons);//排序for (int i = 0; i < persons.length; i++) {for (int j = i+1; j < persons.length; j++) {context.write(new Text(persons[i]+"-"+persons[j]),new Text(friend));}}}}
    

    e:FriendReducer2

    package com.kgf.mapreduce.friend;import java.io.IOException;
    import java.util.HashSet;import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;public class FriendReducer2 extends Reducer<Text, Text, Text, Text> {@Overrideprotected void reduce(Text key, Iterable<Text> values,Context context)throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();HashSet<String> set = new HashSet<String>();for (Text value : values) {String v = value.toString();if(!set.contains(v)) {set.add(v);sb.append(v).append(",");}}sb.deleteCharAt(sb.length()-1);context.write(key, new Text(sb.toString()));}
    }
    

    f:FriendDriver2

    package com.kgf.mapreduce.friend;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 FriendDriver2 {public static void main(String[] args) throws Exception {//1:获取Job对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//2:设置jarjob.setJarByClass(FriendDriver2.class);//3:关联Mapper和reducerjob.setMapperClass(FriengMapper02.class);job.setReducerClass(FriendReducer2.class);//4:设置mapper输出参数job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//5:设置最终输出job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//6:设置文件输入输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));//7:提交boolean result = job.waitForCompletion(true);System.exit(result?0:1);}}
    

MapReduce实现寻找共同好友相关推荐

  1. 用MapReduce实现寻找共同好友(Python)

    1.需求: 给出A-O个人中每个人的好友列表(好友单向),求出哪些人两两之间有共同好友,以及他们的共同好友都有谁. 好友列表数据: A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I ...

  2. 使用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 ...

  3. python版mapreduce题目实现寻找共同好友

    看到一篇不知道是好好玩还是好玩玩童鞋的博客,发现一道好玩的mapreduce题目,地址http://www.cnblogs.com/songhaowan/p/7239578.html 如图 由于自己太 ...

  4. HadoopMapReduce寻找共同好友

    假设有所有用户的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的),如果两个用户之间存在共同好友,需要找出他们之间的共同好友. 样例文本如下: A:B,C,D,F,E ...

  5. MapReduce学习笔记(7)—— 寻找共同好友

    1 数据 冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的).求出哪些人两两之间有共同好友,及他俩的共同好友都有谁? A:B,C,D,F,E,O B:A,C,E,K C:F,A,D ...

  6. MapReduce寻找共同好友

    1.测试文件 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 ...

  7. 黑猴子的家:MapReduce 找微信共同好友分析

    1.数据 https://www.jianshu.com/p/1613f171f466 2.需求 以上是微信的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的) 求出 ...

  8. 算法系列讲解之:社交网络之共同好友模型讲解

    问题导读 1.寻找共同好友,该如何转换为程序逻辑? 2.寻找共同好友的思路是什么? 3.如何通过MapReduce实现寻找共同好友? 我们知道社交网络经常会看到共同好友,共同好友目前资料也非常的多,也 ...

  9. MapReduce实现好友单向推荐

    需求: 现有一份QQ好友数据,其中数据第一列代表用户QQ昵称,第二列代表好友QQ昵称.要求编写MapReduce程序实现QQ好友推荐,例如A的好友是B,B的好友是C,即A与C有共同的好友B,则可以向A ...

最新文章

  1. Udacity机器人软件工程师课程笔记(二十二) - 物体识别 - 色彩直方图,支持向量机SVM
  2. 华为自研操作系统官宣了,但有机会成功吗
  3. 开源的那些事儿之如何看待开源
  4. iptables禁止端口和开放端口
  5. Linux 使用 jstat 命令查看 jvm 的 GC 情况
  6. 删除顽固文件的执行代码,删除rhsa属性文件,删除服务器中黑客留下...
  7. MATLAB编程思想
  8. jax-ws开发的webservice集成到web项目中
  9. oracle中打钩,wps文档如何在小方块里打钩?
  10. matlab斯奈尔定律,斯奈尔定律和Zoeppritz方程
  11. arduino中u8g2汉字显示总结
  12. RPG Maker MV 密码宝箱
  13. fastposter v2.7.0 发布 电商海报编辑器
  14. STM32 GPIOx_CRL/GPIOx_CRH 寄存器的设置的简化描述
  15. 字号与磅值对应关系_终极版式指南:磅值,大写与小写,Em和En破折号等
  16. 在安全创新的道路上飞奔
  17. tomcat的两个错误提示
  18. 致远互联2021年中报:扎根协同的广阔天地,撸起袖子加油干
  19. ElasticSearch设置密码Windows
  20. 渡一教育_Java每日一练:建立Statement的作用是什么、前端Console.log( Boolean(‘‘))输出的是什么、如果希望1监听TCP端口为9000,服务端应该怎样创建socket

热门文章

  1. 利用C语言实现sin(x)曲线与cos(x)曲线图形的同时显示
  2. linux下载nginx
  3. python爬取股票评论_Python爬虫股票评论,snowNLP简单分析股民用户情绪
  4. YOLOv5白皮书-第Y3周:yolov5s.yaml文件解读
  5. 解决twaver 引起的文本不可复制问题
  6. 试论述通货紧缩的经济效应
  7. 川教版初中一年级计算机教案,川教版信息技术教案六年级下册.doc
  8. 卡片机玩出单反机的效果
  9. html纵向滚动条隐藏,隐藏横向滚动条或纵向滚动条的解决方案
  10. 李宏毅老师课程:Unsupervised Learning - Word Embedding