///\\\\\\\\
fri.txt 如下: person: friend1, friend2, friend3, friend4, …..

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

\\\\\\

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;public class friends {static class FriendMapper extends Mapper<LongWritable, Text, Text, Text>{       @Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)throws IOException, InterruptedException {String line = value.toString();String[] person_friends = line.split(":");String person = person_friends[0];String friends = person_friends[1];for(String friend:friends.split(",")) {context.write(new Text(friend), new Text(person));                      }                                                           }           }   static class FriendsReducer extends Reducer<Text, Text, Text, Text>{@Overrideprotected void reduce(Text friend, Iterable<Text> persons, Context context)throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();for(Text person:persons) {              sb.append(person).append(",");                              }           context.write(friend, new Text(sb.toString()));                 }           }                               public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {       Configuration conf =  new Configuration();      Job job = Job.getInstance(conf);job.setJarByClass(friends.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);        job.setMapperClass(FriendMapper.class);job.setReducerClass(FriendsReducer.class);      //  FileInputFormat.setInputPaths(job, new Path("hdfs://Master:9000/data/demon/friends/input"));//  FileOutputFormat.setOutputPath(job, new Path("hdfs://Master:9000//data/demon/friends/output"));     FileInputFormat.setInputPaths(job, new Path("/home/hadoop/examples/friends/input"));FileOutputFormat.setOutputPath(job, new Path("/home/hadoop/examples/friends/out_1"));job.waitForCompletion(true);            }
}

//\\\\
得到结果如下: friend: person1, person2, person3, ….

A   I,K,C,B,G,F,H,O,D,
B   A,F,J,E,
C   A,E,B,H,F,G,K,
D   G,C,K,A,L,F,E,H,
E   G,M,L,H,A,F,B,D,
F   L,M,D,C,G,A,
G   M,
H   O,
I   O,C,
J   O,
K   B,
L   D,E,
M   E,F,
O   A,H,I,J,F,

/\\\\\
///\\\\

import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;public class FriendStepTo {static class FriendToMapper extends Mapper<LongWritable, Text, Text, Text>{ @Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)throws IOException, InterruptedException {String line = value.toString();String[] friend_person = line.split("\t");String friend = friend_person[0];String[] persons = friend_person[1].split(",");Arrays.sort(persons);for(int i=0; i<persons.length-2; i++) {for(int j=i+1; j<persons.length-1; j++) {context.write(new Text(persons[i]+"-" +persons[j]), new Text(friend));                  }                               }       }           }   static class FriendsToReducer extends Reducer<Text, Text, Text, Text>{@Overrideprotected void reduce(Text person_person, Iterable<Text> friends, Context context)throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();for(Text friend:friends) {              sb.append(friend).append(" ");                              }           context.write(person_person, new Text(sb.toString()));                  }           }                               public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf =  new Configuration();      Job job = Job.getInstance(conf);job.setJarByClass(FriendStepTo.class);      job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);        job.setMapperClass(FriendToMapper.class);job.setReducerClass(FriendsToReducer.class);        //  FileInputFormat.setInputPaths(job, new Path("hdfs://Master:9000/data/demon/friends/input"));//  FileOutputFormat.setOutputPath(job, new Path("hdfs://Master:9000//data/demon/friends/output"));     FileInputFormat.setInputPaths(job, new Path("/home/hadoop/examples/friends/out_1"));FileOutputFormat.setOutputPath(job, new Path("/home/hadoop/examples/friends/out_2"));       job.waitForCompletion(true);            }
}

///\\\\\\\\\\\
得到的结果如下: person1 - person2 : friend1, friend2 ,…..

A-B C E
A-C F D
A-D E F
A-E B C D
A-F C D B E O
A-G D E F C
A-H E O C D
A-I O
A-K D
A-L F E
B-C A
B-D E A
B-E C
B-F E A C
B-G C E A
B-H E C A
B-I A
B-K A
B-L E
C-D F A
C-E D
C-F D A
C-G F A D
C-H A D
C-I A
C-K D A
C-L F
D-F E A
D-G A E F
D-H A E
D-I A
D-K A
D-L F E
E-F C D B
E-G D C
E-H D C
E-K D
F-G C E D A
F-H C A D E O
F-I A O
F-K D A
F-L E
G-H D E C A
G-I A
G-K A D
G-L F E
H-I A O
H-K A D
H-L E
I-K A 

MapReduce例子——找出QQ共同好友相关推荐

  1. mapreduce应用-找出扣扣共同好友

    需求:找出扣扣共同好友 用户:好友,好友2,- 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, ...

  2. 找出QQ共同好友的实现

    以下是qq的好友列表数据,冒号前是一个用,冒号后是该用户的所有好友(好友关系是单向的,也就是说A是B好友,B不一定是A好友) A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A ...

  3. hadoop找出QQ共同好友算法实现

    背景 A:B,C,D,E,F 表示A有bcdef好友 B:C,D,H,Y 以上可知道AB的共同好友为CD 思路: 1:我们先找出一个人被哪几个人共同拥有 测试数据: 2:第一阶段mr程序: packa ...

  4. 准确找出QQ群里长期潜水的成员踢出去(转)

    准确找出QQ群里长期潜水的成员踢出去(转) QQ群解除了以前不能多人同时聊天的限制,多个好友可以在群中即时交流.笔者也建立了一个IT作者编辑群,随着加入人数的增多,慢慢达到了群人数的上限,不能够再加入 ...

  5. 快速找出QQ群成员中不在名单内的人

    快速找出QQ群成员中不在名单内的人 Created: Aug 15, 2020 10:21 PM Tags: Python, 计划中 Updated: Aug 17, 2020 10:45 PM 需求 ...

  6. Python脚本一键找出哪些微信好友删了你(附源码)

    查看被删的微信好友 原理就是新建群组,如果加不进来就是被删好友了(不要在群组里讲话,别人是看不见的) 用的是微信网页版的接口 查询结果可能会引起一些心理上的不适,请小心使用-(逃 还有些小问题: 结果 ...

  7. Hadoop MapReduce V2——找出每个月气温最高的2天

    项目目录 MyTQ package com.henu.tq; import java.io.IOException; import org.apache.hadoop.conf.Configurati ...

  8. 利用Python找出QQ空间把你屏蔽的人

    目录 前言 分析(x0 整体思路分析) 分析(x1 好友数据的获取) 准备工作 使用到的模块 模块的安装 插件的安装 分析(x0) 分析(x2) 分析(x3) 代码 分析(x3 获取屏蔽的好友) 前言 ...

  9. 利用Python找出QQ空间把你屏蔽的人,面试经历分享

    分析(x0) 分析(x2) 分析(x3) 代码 [分析(x3 获取屏蔽的好友)](about:blank#%E5%88%86%E6%9E%90%EF%BC%88x3%20%E8%8E%B7%E5%8 ...

最新文章

  1. 点云滤波/分割/关键点提取/配准/识别/重建教程
  2. JavaScript HTML DOM - 改变 CSS
  3. Django之Form组件
  4. Lucene.Net---1索引的建立
  5. 【深度学习】收藏|神经网络调试Checklist
  6. C++类的Const数组的初始化
  7. pycharm下配置jupyter_在 Pycharm 中安装及使用 Jupyter (图文详解)
  8. [Reomting Debug] 巧用VS 的remote debug 功能远程调试程序 经验分享.
  9. linux mmap系统调用
  10. python 商城_python 开源商城安装
  11. 嵌入式Linux系统编程学习之三十三网络相关概念
  12. win10系统服务器管理器,win10服务管理器,详细教您Win10服务管理器怎样打开
  13. js 只准输入数字_javascript 限制只允许输入数字的几种方法
  14. 微信pc端window10多开应用
  15. 集体照的拍摄与后期合成处理
  16. 应用程序正常初始化(0xc150002)失败
  17. Citrix虚拟桌面部署
  18. java版铁傀儡刷新机制,我的世界:新版村庄的铁傀儡数量都快赶上村民了?刷新效率很高!...
  19. 霍尔传感器磁极的磁场强度计算
  20. 36岁互联网高管从大厂裸辞,专门卖书快乐多了

热门文章

  1. IT界春联,就是辣么扎心!
  2. override关键字
  3. spotify使用教程_什么是Spotify Kids? (以及如何使用其家长控制)
  4. 面试前必备技能get:如何知彼?
  5. 计算机专业应聘人事专员,hr人事专员面试经验 - 共203条真实hr人事专员面试经验分享 - 职业圈...
  6. 取得助工前发表的论文可以用来评中级吗?
  7. 【Unity游戏开发】TimelinePlayable结合使用,Track自定义Timeline轨道
  8. mysql查询两张表的同一列_如何快速查找两个数据表之间的相同和不同?
  9. AI作画—赛博朋克你听过嘛
  10. rabbitmq消息队列入门到整合springboot(篇幅较长内容详细)