MapReduce分析共同好友
来源:互联网 发布:程序员工资最高多少 编辑:程序博客网 时间:2024/05/19 17:26
两次使用mapreduce
输入文件:
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–B: C E
思路分析:从结论出发,最后一步的reduce 的key应为
A–B: c A–B: e
由此,改形式应该为第一次mapreduce的输出结果
所以进行两次mapreduce即可
FriendsPre.java:
public class FriendsPre { static public class FriendsMapper extends Mapper<LongWritable, Text, Text, Text> { Text friendText = new Text(); Text usrText = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { //获取每一行的好友内容 String line = value.toString(); String[] spil1 = line.split(":"); //当前用户 String usr = spil1[0]; //用户的所有好友 String[] friends = spil1[1].split(","); //以该用户为为value,好友为key,输出给map for (String friend:friends ) { friendText.set(friend); usrText.set(usr); context.write(friendText, usrText); } } } static public class FriendsReducer extends Reducer<Text, Text, Text, NullWritable> { Text usrText2 = new Text(); @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { //将iterator转化为list,因为iterator没有get方法,无法操作下标 List<Text> usersList = new ArrayList<Text>(); for (Text usr : values) { //Text usrText = new Text(); //Text usrText = new Text(); Text usrText = new Text(usr); usersList.add(usrText); } for (int i = 0; i < usersList.size() - 1; i++) { for (int j = i + 1; j < usersList.size(); j++) { //生成关系字符串如:a--b:c usrText2.set(usersList.get(i).toString() + "--"+usersList.get(j).toString()+":"+key.toString()); context.write(usrText2, NullWritable.get()); } } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(FriendsPre.class); job.setJar("/Users/inequality/IdeaProjects/mapr/out/artifacts/mapr_jar/mapr.jar"); //设置调用类 job.setMapperClass(FriendsMapper.class); job.setReducerClass(FriendsReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(NullWritable.class); //指定输入输入输出路径 FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); FileSystem fs = FileSystem.get(conf); if (fs.exists(new Path(args[1]))) { fs.delete(new Path(args[1]),true); } //将job中配置的相关参数,以及job所用的java类所用的jar包,提交给yarn去运行 //job.submit(); boolean res = job.waitForCompletion(true); System.exit(res ? 0 : 1); }}
FirendsLast.java
public class FriendsLast { static class FriendsLastMapper extends Mapper<LongWritable, Text, Text, Text> { Text relationText = new Text(); Text friendText = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = line.split(":"); //关系字符串 String relation = words[0]; //共同好友 String friend = words[1]; //输出 relationText.set(relation); friendText.set(friend); context.write(relationText, friendText); } } static class FriendsLastReducer extends Reducer<Text, Text, Text, NullWritable> { Text re = new Text(); @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { StringBuffer reSb = new StringBuffer(key.toString()); reSb.append(": ");//a--b: for (Text friend : values) { reSb.append(friend.toString()).append(" "); } re.set(reSb.toString()); context.write(re, NullWritable.get()); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(FriendsPre.class); job.setJar("/Users/inequality/IdeaProjects/mapr/out/artifacts/mapr_jar/mapr.jar"); //设置调用类 job.setMapperClass(FriendsLastMapper.class); job.setReducerClass(FriendsLastReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(NullWritable.class); //指定输入输入输出路径 FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); FileSystem fs = FileSystem.get(conf); if (fs.exists(new Path(args[1]))) { fs.delete(new Path(args[1]),true); } //将job中配置的相关参数,以及job所用的java类所用的jar包,提交给yarn去运行 //job.submit(); boolean res = job.waitForCompletion(true); System.exit(res ? 0 : 1); }
阅读全文
0 0
- MapReduce分析共同好友
- MapReduce:共同好友
- Mapreduce实例---共同好友
- MapReduce寻找共同好友
- MapReduce找共同好友
- MapReduce找共同好友
- MapReduce实现寻找共同好友
- MapReduce--6--求共同好友
- mapreduce系列(7)--查找共同好友
- MapReduce实战练习四:找出共同好友
- mapreduce应用-找出扣扣共同好友
- 数据分析之共同好友统计
- 使用MapReduce实现寻找共同好友的案例
- MapReduce--7--求共同好友--改进版JobControl
- MapReduce——求两个人的共同好友算法
- 共同好友
- 共同好友
- 社交粉丝数据分析----求qq共同好友
- 微服务架构:Eureka参数配置项详解
- 超大扫描件转为可编辑WordJ技巧分享
- MYSQL根据热门条件进行排序分页,防止分页有重复数据
- jsp中返回上一个页面,但不刷新数据
- 聊天界面软件盘遮挡布局的解决
- MapReduce分析共同好友
- 机器学习算法与Python实践(5)
- java线程安全相关
- python函数小结归纳
- 每天进步一点点(电路篇)-固态汽车电路稳压器
- window.open常用参数具体意义
- Git版本库的概念/创建
- Android修改状态栏样式
- 08-天亮大数据系列教程之python streaming编程