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
需求:
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的时候,将结果合并即可
源代码如下
第一题:
package Demo5; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
* @author 星际毁灭
* 找出ABC等人的好友
* */
import Demo4.Log1; public class Friend1 {
public static class Map extends Mapper<Object , Text , Text , Text>{
private static Text newKey=new Text();
private static final IntWritable one = new IntWritable(1);
public void map(Object key,Text value,Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException{
String line=value.toString();
String[] array=line.split(":"); //数据实例 A:B,C,D,F,E,O
String first=array[0]; //冒号前边部分
String second=array[1]; //冒号后边部分
String[] friend=second.split(","); //冒号后边部分在切割
System.out.println(first+"\t"+second);
for(int i=0;i<friend.length;i++) {
context.write(new Text(friend[i]),new Text(first)); //好友关系,即源文件的反向
} }
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
String res="";
for(Text val:values) {
res+=val.toString()+","; //将结果合并
}
context.write(key,new Text(res));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
Configuration conf=new Configuration();
Path in=new Path("hdfs://192.168.6.132:9000/wys/in/friend1.txt");
Path out=new Path("hdfs://192.168.6.132:9000/wys/out/friend1"); Job job =new Job(conf,"OneSort"); FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
job.setJarByClass(Friend1.class);
job.setMapperClass(Friend1.Map.class);
job.setReducerClass(Friend1.Reduce.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
第二题:
package Demo5; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* @author 王翌淞
*
* 求出他们的共同好友
* */
public class Friend2 {
public static class Map extends Mapper<Object , Text , Text , Text>{
private static Text newKey=new Text();
private static final IntWritable one = new IntWritable(1);
public void map(Object key,Text value,Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException{
String line=value.toString();
String[] array=line.split("\t");
String first=array[0];
String second=array[1];
String[] friend=second.split(",");
System.out.println(first+"\t"+second); //通过两层的for循环,A的好友进行组合,有顺序
for(int i=0;i<friend.length;i++) {
for(int j=i+1;j<friend.length;j++) {
context.write(new Text(friend[i]+"-"+friend[j]),new Text(first)); //正序关系
context.write(new Text(friend[j]+"-"+friend[i]),new Text(first)); //倒序关系
} }
//context.write(new Text(friend[i]),new Text(first)); }
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
String res="";
for(Text val:values) {
res+=val.toString()+" ";
}
context.write(key,new Text(res));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
Configuration conf=new Configuration();
Path in=new Path("hdfs://192.168.6.132:9000/wys/out/friend1/part-r-00000");
Path out=new Path("hdfs://192.168.6.132:9000/wys/out/friend2"); Job job =new Job(conf,"OneSort"); FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
job.setJarByClass(Friend2.class);
job.setMapperClass(Friend2.Map.class);
job.setReducerClass(Friend2.Reduce.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
Mapreduce案例之找共同好友的更多相关文章
- 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 ...
- mapreduce案例:获取PI的值
mapreduce案例:获取PI的值 * content:核心思想是向以(0,0),(0,1),(1,0),(1,1)为顶点的正方形中投掷随机点. * 统计(0.5,0.5)为圆心的单位圆中落点占总落 ...
- 【Hadoop离线基础总结】MapReduce案例之自定义groupingComparator
MapReduce案例之自定义groupingComparator 求取Top 1的数据 需求 求出每一个订单中成交金额最大的一笔交易 订单id 商品id 成交金额 Order_0000005 Pdt ...
- MapReduce案例-好友推荐
用过各种社交平台(如QQ.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...
- 【Hadoop学习之十】MapReduce案例分析二-好友推荐
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...
- MapReduce案例二:好友推荐
1.需求 推荐好友的好友 图1: 2.解决思路 3.代码 3.1MyFoF类代码 说明: 该类定义了所加载的配置,以及执行的map,reduce程序所需要加载运行的类 package com.hado ...
- 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐
案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...
- 【尚学堂·Hadoop学习】MapReduce案例1--天气
案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...
- Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况
mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...
随机推荐
- 你必须知道的495个c语言问题(笔记)
1.1我该如何决定使用哪种整数类型? 用到较大的数用long:空间很重要(例如有很大的数组或很多的结构)用short:此外用int. win32: int 32bit 4byte char 8b ...
- UWP笔记-边框背景虚化效果
这是一个简单的UI外观 1.添加Negut包: Microsoft.Toolkit.Uwp.UI.Controls 2.xaml:命名空间中引用 xmlns:controls="using: ...
- Docker 安装 Tomcat
查找Docker Hub上的tomcat镜像 docker search tomcat 取官方的镜像 docker pull tomcat 使用tomcat镜像 创建目录tomcat,用于存放后面的相 ...
- 数据库连接池——C3P0&Druid(快速入门)
数据库连接池--C3P0&Druid (一) 数据库连接池 每一个事物都有其存在的意义,在初学jdbc的时候,我们建立数据库连接对象后,会对其进行释放,但是数据库连接的建立和关闭是非常消耗资源 ...
- [转帖]windows CIFS sabma协议识
windows CIFS sabma协议识别 https://www.cnblogs.com/tcicy/p/9992871.html 公司的一个共享服务器就是 win2003的 mount 的时候 ...
- ubuntu18下Docker运行springboot项目
1.springboot项目打成jar包 mvn install 2.编写Dockerfile # 基础镜像使用java FROM java:8 # 作者 #MAINTAINER sk # VOLUM ...
- 剑指offer20:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
1 题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 2. 思路和方法 利用辅助栈来存储现有栈的最小值.在入栈和出栈的时候将现有栈和最小 ...
- Python Des加密与解密实现软件注册码、机器码
原理 判断路径下是否存在识别文件,若存在就解密对比,若不存在就进入机器码注册: 获取系统C盘序列号作为识别ID,并添加随机数作为混淆,生成最终机器码. 将机器码发给软件开发者,开发者将机器码解密后,添 ...
- 第二章、http协议及嗅探抓包--http协议详解
初识http协议 hypertext trandfer protocol 超文本传输协议,是一种分布式,合作式,多媒体信息系统服务,面向应用层的协议.使用最广泛的应用层协议,基于传输层的TCP协 ...
- docker 入门2 - 容器 【翻译】
入门,第 2 部分:容器 先决条件 安装的 Docker 版本是 1.13 及以上. 读完 第一部分 用下面的命令快速测试你的环境是否完备: docker run hello-world 概述 现在开 ...