【Hadoop学习之十】MapReduce案例分析二-好友推荐
环境
虚拟机:VMware 10
Linux版本:CentOS-6.5-x86_64
客户端:Xshell4
FTP:Xftp4
jdk8
hadoop-3.1.1
最应该推荐的好友TopN,如何排名?



tom hello hadoop cat
world hadoop hello hive
cat tom hive
mr hive hello
hive cat hadoop world hello mr
hadoop tom hive world
hello tom world hive mr
package test.mr.fof; 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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class MyFOF { /**
* 最应该推荐的好友TopN,如何排名? * @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Configuration conf = new Configuration(true);
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
conf.set("sleep", otherArgs[2]); Job job = Job.getInstance(conf,"FOF");
job.setJarByClass(MyFOF.class); //Map
job.setMapperClass(FMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); //Reduce
job.setReducerClass(FReducer.class); //HDFS 输入路径
Path input = new Path(otherArgs[0]);
FileInputFormat.addInputPath(job, input );
//HDFS 输出路径
Path output = new Path(otherArgs[1]);
if(output.getFileSystem(conf).exists(output)){
output.getFileSystem(conf).delete(output,true);
}
FileOutputFormat.setOutputPath(job, output ); System.exit(job.waitForCompletion(true) ? 0 :1);
}
// tom hello hadoop cat
// world hadoop hello hive
// cat tom hive
// mr hive hello
// hive cat hadoop world hello mr
// hadoop tom hive world
// hello tom world hive mr }
package test.mr.fof; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils; public class FMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ Text mkey= new Text();
IntWritable mval = new IntWritable(); @Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException { //value: 0-直接关系 1-间接关系
//tom hello hadoop cat : hello:hello 1
//hello tom world hive mr hello:hello 0 String[] strs = StringUtils.split(value.toString(), ' '); String user=strs[0];
String user01=null;
for(int i=1;i<strs.length;i++){
//与好友清单中好友属于直接关系
mkey.set(fof(strs[0],strs[i]));
mval.set(0);
context.write(mkey, mval); for (int j = i+1; j < strs.length; j++) {
Thread.sleep(context.getConfiguration().getInt("sleep", 0));
//好友列表内 成员之间是间接关系
mkey.set(fof(strs[i],strs[j]));
mval.set(1);
context.write(mkey, mval);
}
}
} public static String fof(String str1 , String str2){ if(str1.compareTo(str2) > 0){
//hello,hadoop
return str2+":"+str1;
}
//hadoop,hello
return str1+":"+str2;
} }
package test.mr.fof; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class FReducer extends Reducer<Text, IntWritable, Text, Text> { Text rval = new Text();
@Override
protected void reduce(Text key, Iterable<IntWritable> vals, Context context)
throws IOException, InterruptedException
{
//是简单的好友列表的差集吗?
//最应该推荐的好友TopN,如何排名? //hadoop:hello 1
//hadoop:hello 0
//hadoop:hello 1
//hadoop:hello 1
int sum=0;
int flg=0;
for (IntWritable v : vals)
{
//0为直接关系
if(v.get()==0){
//hadoop:hello 0
flg=1;
}
sum += v.get();
} //只有间接关系才会被输出
if(flg==0){
rval.set(sum+"");
context.write(key, rval);
}
}
}
【Hadoop学习之十】MapReduce案例分析二-好友推荐的更多相关文章
- MapReduce深度分析(二)
MapReduce深度分析(二) 五.JobTracker分析 JobTracker是hadoop的重要的后台守护进程之一,主要的功能是管理任务调度.管理TaskTracker.监控作业执行.运行作业 ...
- Hadoop学习笔记—20.网站日志分析项目案例(二)数据清洗
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:当前页面 网站日志分析项目案例 ...
- 【Hadoop学习之十三】MapReduce案例分析五-ItemCF
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 推荐系统——协同过滤(Collab ...
- Hadoop学习笔记—20.网站日志分析项目案例(一)项目介绍
网站日志分析项目案例(一)项目介绍:当前页面 网站日志分析项目案例(二)数据清洗:http://www.cnblogs.com/edisonchou/p/4458219.html 网站日志分析项目案例 ...
- Hadoop学习笔记—20.网站日志分析项目案例(三)统计分析
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:http://www.cnbl ...
- [b0012] Hadoop 版hello word mapreduce wordcount 运行(二)
目的: 学习Hadoop mapreduce 开发环境eclipse windows下的搭建 环境: Winows 7 64 eclipse 直接连接hadoop运行的环境已经搭建好,结果输出到ecl ...
- 【第二课】kaggle案例分析二
Evernote Export 推荐系统比赛(常见比赛) 推荐系统分类 最能变现的机器学习应用 基于应用领域分类:电子商务推荐,社交好友推荐,搜索引擎推荐,信息内容推荐等 **基于设计思想:**基于协 ...
- 【Hadoop学习之十二】MapReduce案例分析四-TF-IDF
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 概念TF-IDF(term fre ...
- 【Hadoop学习之九】MapReduce案例分析一-天气
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 找出每个月气温最高的2天 1949 ...
随机推荐
- CF718C Sasha and Array 线段树+矩阵加速
正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...
- 【socket-python应用】控制泓格ET-7044通信模块输入DI输出DO
socket-python应用:控制泓格ET-7044通信模块输入DI输出DO 本节主要内容: 1.socket-python建立TCP通信 2.配合泓格通信模块说明书,查看输入输出寄存器地址,发送指 ...
- <<Sklearn 与 TensorFlow 机器学习实用指南>>
地址 https://github.com/apachecn/hands-on-ml-zh 目录结构 零.前言 第一部分 机器学习基础 一.机器学习概览 二.一个完整的机器学习项目 三.分类 四.训练 ...
- WCF访问超时:HTTP 请求已超过xx:yy分配的超时。为此操作分配的时间可能是较长超时的一部分。
在服务端设置时间长些 <client> <endpoint address="http://43.98.49.189:5700/UPJWCFServcie.svc" ...
- cd 命令
[root@localhost ~]# cd # 进入当前用户的家目录 [root@localhost ~]# cd ~ # 进入当前用户的家目录 [root@localhost ~]# cd /da ...
- left outer join的on不起作用
left outer join的on不起作用 Why and when a LEFT JOIN with condition in WHERE clause is not equivalent to ...
- mysql在linux上的安装
前提: 环境:workstation 11 + CentOS 7 + mysql-5.6.40 安装前先查看服务器里是否有老版本的mysql已经被安装了 rpm -qa|grep mysql 如果有就 ...
- NYOJ 92
1.深搜(会爆栈,通过开全局栈模拟递归) 爆栈代码 # include<iostream> # include<string> # include<string.h> ...
- Centos7 下 yum -y install ntp 出现/var/run/yum.pid 已被锁定
[root@localhost ~ ]# yum -y install ntp已加载插件:fastestmirror, langpacksRepodata is over 2 weeks old. I ...
- 继承:继承后子类构造函数具有隐式super,所以子类中所以的构造函数默认会访问父类中的空参数的构造函数
class Test { Test(){ System.out.println("Test"); } Test(String name){ System.out.println(& ...