数据准备:

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

需求:

1.先求出A、B、C、….等是谁的好友

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

需求解读:

1.有题目可得,该关系为单项关系可以理解为关注,即A关注的为BCDEF,B关注的为AK,所以求A B C...是谁的关注,即需要将冒号后边的作为key,前边的作为value进行map,在reduce的时候在合并即可。

2.求两两之间的共同关注,这时我们需要借助1题产生的结果文件,map是从后边的value值两两组合生成两两关系。作为新的key值输入,新的value为1题的key。

reduce的时候,将结果合并即可

源代码如下

第一题:

  1. package Demo5;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. import org.apache.hadoop.mapreduce.Reducer.Context;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15. /**
  16. * @author 星际毁灭
  17. * 找出ABC等人的好友
  18. * */
  19. import Demo4.Log1;
  20.  
  21. public class Friend1 {
  22. public static class Map extends Mapper<Object , Text , Text , Text>{
  23. private static Text newKey=new Text();
  24. private static final IntWritable one = new IntWritable(1);
  25. public void map(Object key,Text value,Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException{
  26. String line=value.toString();
  27. String[] array=line.split(":"); //数据实例 A:B,C,D,F,E,O
  28. String first=array[0]; //冒号前边部分
  29. String second=array[1]; //冒号后边部分
  30. String[] friend=second.split(","); //冒号后边部分在切割
  31. System.out.println(first+"\t"+second);
  32. for(int i=0;i<friend.length;i++) {
  33. context.write(new Text(friend[i]),new Text(first)); //好友关系,即源文件的反向
  34. }
  35.  
  36. }
  37. }
  38. public static class Reduce extends Reducer<Text, Text, Text, Text>{
  39. public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
  40. String res="";
  41. for(Text val:values) {
  42. res+=val.toString()+","; //将结果合并
  43. }
  44. context.write(key,new Text(res));
  45. }
  46. }
  47. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
  48. System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
  49. Configuration conf=new Configuration();
  50. Path in=new Path("hdfs://192.168.6.132:9000/wys/in/friend1.txt");
  51. Path out=new Path("hdfs://192.168.6.132:9000/wys/out/friend1");
  52.  
  53. Job job =new Job(conf,"OneSort");
  54.  
  55. FileInputFormat.addInputPath(job,in);
  56. FileOutputFormat.setOutputPath(job,out);
  57. job.setJarByClass(Friend1.class);
  58. job.setMapperClass(Friend1.Map.class);
  59. job.setReducerClass(Friend1.Reduce.class);
  60.  
  61. job.setOutputKeyClass(Text.class);
  62. job.setOutputValueClass(Text.class);
  63. job.waitForCompletion(true);
  64.  
  65. System.exit(job.waitForCompletion(true) ? 0 : 1);
  66. }
  67.  
  68. }

第二题:

  1. package Demo5;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. import org.apache.hadoop.mapreduce.Reducer.Context;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15.  
  16. /**
  17. * @author 王翌淞
  18. *
  19. * 求出他们的共同好友
  20. * */
  21. public class Friend2 {
  22. public static class Map extends Mapper<Object , Text , Text , Text>{
  23. private static Text newKey=new Text();
  24. private static final IntWritable one = new IntWritable(1);
  25. public void map(Object key,Text value,Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException{
  26. String line=value.toString();
  27. String[] array=line.split("\t");
  28. String first=array[0];
  29. String second=array[1];
  30. String[] friend=second.split(",");
  31. System.out.println(first+"\t"+second);
  32.  
  33. //通过两层的for循环,A的好友进行组合,有顺序
  34. for(int i=0;i<friend.length;i++) {
  35. for(int j=i+1;j<friend.length;j++) {
  36. context.write(new Text(friend[i]+"-"+friend[j]),new Text(first)); //正序关系
  37. context.write(new Text(friend[j]+"-"+friend[i]),new Text(first)); //倒序关系
  38. }
  39.  
  40. }
  41. //context.write(new Text(friend[i]),new Text(first));
  42.  
  43. }
  44. }
  45. public static class Reduce extends Reducer<Text, Text, Text, Text>{
  46. public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
  47. String res="";
  48. for(Text val:values) {
  49. res+=val.toString()+" ";
  50. }
  51. context.write(key,new Text(res));
  52. }
  53. }
  54. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
  55. System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
  56. Configuration conf=new Configuration();
  57. Path in=new Path("hdfs://192.168.6.132:9000/wys/out/friend1/part-r-00000");
  58. Path out=new Path("hdfs://192.168.6.132:9000/wys/out/friend2");
  59.  
  60. Job job =new Job(conf,"OneSort");
  61.  
  62. FileInputFormat.addInputPath(job,in);
  63. FileOutputFormat.setOutputPath(job,out);
  64. job.setJarByClass(Friend2.class);
  65. job.setMapperClass(Friend2.Map.class);
  66. job.setReducerClass(Friend2.Reduce.class);
  67.  
  68. job.setOutputKeyClass(Text.class);
  69. job.setOutputValueClass(Text.class);
  70. job.waitForCompletion(true);
  71.  
  72. System.exit(job.waitForCompletion(true) ? 0 : 1);
  73. }
  74.  
  75. }

Mapreduce案例之找共同好友的更多相关文章

  1. MapReduce案例:统计共同好友+订单表多表合并+求每个订单中最贵的商品

    案例三: 统计共同好友 任务需求: 如下的文本, A:B,C,D,F,E,OB:A,C,E,KC:F,A,D,ID:A,E,F,LE:B,C,D,M,LF:A,B,C,D,E,O,MG:A,C,D,E ...

  2. mapreduce案例:获取PI的值

    mapreduce案例:获取PI的值 * content:核心思想是向以(0,0),(0,1),(1,0),(1,1)为顶点的正方形中投掷随机点. * 统计(0.5,0.5)为圆心的单位圆中落点占总落 ...

  3. 【Hadoop离线基础总结】MapReduce案例之自定义groupingComparator

    MapReduce案例之自定义groupingComparator 求取Top 1的数据 需求 求出每一个订单中成交金额最大的一笔交易 订单id 商品id 成交金额 Order_0000005 Pdt ...

  4. MapReduce案例-好友推荐

    用过各种社交平台(如QQ.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...

  5. 【Hadoop学习之十】MapReduce案例分析二-好友推荐

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...

  6. MapReduce案例二:好友推荐

    1.需求 推荐好友的好友 图1: 2.解决思路 3.代码 3.1MyFoF类代码 说明: 该类定义了所加载的配置,以及执行的map,reduce程序所需要加载运行的类 package com.hado ...

  7. 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐

    案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...

  8. 【尚学堂·Hadoop学习】MapReduce案例1--天气

    案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...

  9. Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况

    mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...

随机推荐

  1. 36.HTTP协议

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  2. Java总复习内容

    StringBuffer定义时需要用正确的方式 例如: StringBuffer xxx = new StringBuffer("雯雯是猪"); 使用StringBuffer的连接 ...

  3. sql server中实现mysql的find_in_set函数

    charindex(','+'test'+',',','+Picture+',')>0

  4. Scala当中parallelize并行化的用法

    [学习笔记] parallelize并行化集合是根据一个已经存在的Scala集合创建的RDD对象.集合的里面的元素将会被拷贝进入新创建出的一个可被并行操作的分布式数据集.例如:val rdd03 = ...

  5. 【jmeter测试范例】001——TCP测试

    1.打开Jmeter(或者运行NewDriver.java启动Jmeter) 2.新建一个测试计划 ······ 3.新建线程组 4.设置线程组的参数 1.线程的数量 2.要在多久内完成,即每个请求发 ...

  6. Python实现二叉树的非递归中序遍历

    思路: 1. 使用一个栈保存结点(列表实现): 2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空: 3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树: 4. 栈不为空 ...

  7. git 使用中报错:LF will be replaced by CRLF in app.json

    git config --global core.autocrlf false //禁用自动转换

  8. 11-MySQL DBA笔记-MySQL的监控

    第11章 MySQL的监控 为什么我们需要监控呢?因为如果没有了监控,那么我们的服务可用性就无从度量,我们也无法及时地发现问题和处理问题.一个完善的监控体系,不仅需要进行实时的监控,也需要分析历史的监 ...

  9. MQTT协议探究(二)

    1 回顾与本次目标 1.1 回顾 MQTT控制报文的基本格式 WireShark进行抓包分析了报文 报文分析: CONNECT--连接服务器 CONNACK--确认连接请求 PINGREQ--心跳请求 ...

  10. linux文件目录详细介绍

    linux文件目录 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里 /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点 ...