hadoop入门6:hadoop查询两两之间有共同好友,及他俩的共同好友都是谁

2024-06-07 12:32

本文主要是介绍hadoop入门6:hadoop查询两两之间有共同好友,及他俩的共同好友都是谁,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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,D,F,E,O好友;B有A,C,E,K好友,以此类推;

求两两之间有共同好友,及他俩的共同好友都是谁,例如:A和B之间共同好友是:C、E

编码思路:

       第一步是可以把好友当作key,value是拥有key好友的用户,例如:拥有好友B的是:A,F,J,E用户

       第二步在第一步结果后,双重for循环进行两两之间进行拼接,这样就可以得出正确结果

 

具体代码实现:

第一步:

package com.zsy.mr.commonfriend;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.lang.StringUtils;
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.Job;
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;public class commonFriendStepOne {static class commonFriendStepOneMapper extends Mapper<LongWritable, Text, Text, Text>{Text k = new Text();Text v = new Text();@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)throws IOException, InterruptedException {//通过过冒号分割String[] splits = value.toString().split(":");//获取拥有好友的用户名String name = splits[0];//获取该用户下的好友列表String[] friends = StringUtils.isNotBlank(splits[1])?  splits[1].split(","):null;if(friends != null) {//循环好友,好友当作key,拥有好友的用户名当作valuefor (String friend : friends) {k.set(friend);v.set(name);context.write(k, v);}}}}static class commonFriendStepOneReducer extends Reducer<Text, Text, Text, Text>{Text v = new Text();@Overrideprotected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)throws IOException, InterruptedException {List<String> resultList = new ArrayList<String>();//实际生产代码不建议用list接收,应该是直接处理掉//处理数据,该数据是拥有key好友的所有用户for (Text value : values) {resultList.add(value.toString());}v.set(StringUtils.join(resultList, ","));context.write(key, v);}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();/*conf.set("mapreduce.framework.name", "yarn");conf.set("yarn.resoucemanger.hostname", "hadoop01");*/Job job = Job.getInstance(conf);job.setJarByClass(commonFriendStepOne.class);//指定本业务job要使用的业务类job.setMapperClass(commonFriendStepOneMapper.class);job.setReducerClass(commonFriendStepOneReducer.class);//指定mapper输出的k v类型  如果map的输出和reduce的输出一样,只需要设置输出即可//job.setMapOutputKeyClass(Text.class);//job.setMapOutputValueClass(IntWritable.class);//指定最终输出kv类型(reduce输出类型)job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//指定job的输入文件所在目录FileInputFormat.setInputPaths(job, new Path(args[0]));//指定job的输出结果目录FileOutputFormat.setOutputPath(job, new Path(args[1]));//将job中配置的相关参数,以及job所有的java类所在 的jar包,提交给yarn去运行//job.submit();无结果返回,建议不使用它boolean res = job.waitForCompletion(true);System.exit(res?0:1);}
}

结果:

 

第二步:

代码实现

package com.zsy.mr.commonfriend;import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
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;public class commonFriendStepTwo {static class commonFriendStepTwoMapper extends Mapper<LongWritable, Text, Text, Text>{Text k = new Text();Text v = new Text();@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)throws IOException, InterruptedException {String[] splits = value.toString().split("\t");//获取好友String friend = splits[0];//获取拥有该好友所有的用户信息String[] names = splits[1].split(",");//进行排序,防止计算数据重复,例如:A-B和B-A其实一个对Arrays.sort(names);//进行双重for循环for (int i = 0; i < names.length-1; i++) {String string = names[i];for (int j = i+1; j < names.length; j++) {String string2 = names[j];k.set(string+"-"+string2);v.set(friend);context.write(k, v);}}}}static class commonFriendStepTwoReducer extends Reducer<Text, Text, Text, NullWritable>{Text k = new Text();@Overrideprotected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, Text, NullWritable>.Context context)throws IOException, InterruptedException {List<String> resultList = new ArrayList<String>();//实际生产代码不建议用list接收,应该是直接处理掉for (Text text : value) {resultList.add(text.toString());}k.set(key.toString()+":"+ StringUtils.join(resultList,","));context.write(k, NullWritable.get());}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();/*conf.set("mapreduce.framework.name", "yarn");conf.set("yarn.resoucemanger.hostname", "hadoop01");*/Job job = Job.getInstance(conf);job.setJarByClass(commonFriendStepTwo.class);//指定本业务job要使用的业务类job.setMapperClass(commonFriendStepTwoMapper.class);job.setReducerClass(commonFriendStepTwoReducer.class);//指定mapper输出的k v类型  如果map的输出和reduce的输出一样,只需要设置输出即可job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//指定最终输出kv类型(reduce输出类型)job.setOutputKeyClass(Text.class);job.setOutputValueClass(NullWritable.class);//指定job的输入文件所在目录FileInputFormat.setInputPaths(job, new Path(args[0]));//指定job的输出结果目录FileOutputFormat.setOutputPath(job, new Path(args[1]));//将job中配置的相关参数,以及job所有的java类所在 的jar包,提交给yarn去运行//job.submit();无结果返回,建议不使用它boolean res = job.waitForCompletion(true);System.exit(res?0:1);}
}

结果:

这样就可以找到正确结果

这篇关于hadoop入门6:hadoop查询两两之间有共同好友,及他俩的共同好友都是谁的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1039182

相关文章

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

XML重复查询一条Sql语句的解决方法

《XML重复查询一条Sql语句的解决方法》文章分析了XML重复查询与日志失效问题,指出因DTO缺少@Data注解导致日志无法格式化、空指针风险及参数穿透,进而引发性能灾难,解决方案为在Controll... 目录一、核心问题:从SQL重复执行到日志失效二、根因剖析:DTO断裂引发的级联故障三、解决方案:修复