19-hadoop-fof好友推荐
好友推荐的案例, 需要两个job, 第一个进行好友关系度计算, 第二个job将计算的关系进行推荐
1, fof关系类
package com.wenbronk.friend; import org.apache.hadoop.io.Text; /**
* 定义fof关系
* @author root
*
*/
public class Fof extends Text{ public Fof() {
super();
} /**'
* 不论谁在前,返回一致的顺序
* @param a
* @param b
*/
public Fof(String a, String b) {
super(getFof(a, b));
} /**
* 按字典顺序排序, 保证两个fof为同一组输出
* @param a
* @param b
* @return
*/
public static String getFof(String a, String b) {
int r = a.compareTo(b);
if (r < ) {
return a + "\t" + b;
}else {
return b + "\t" + a;
} } }
2, user类
package com.wenbronk.friend; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class User implements WritableComparable<User>{ private String uname;
private int friedsCount; public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} public int getFriedsCount() {
return friedsCount;
} public void setFriedsCount(int friedsCount) {
this.friedsCount = friedsCount;
} public User() {
super();
} public User(String uname, int friedsCount) {
super();
this.uname = uname;
this.friedsCount = friedsCount;
} @Override
public void readFields(DataInput arg0) throws IOException {
this.uname = arg0.readUTF();
this.friedsCount = arg0.readInt();
} @Override
public void write(DataOutput arg0) throws IOException {
arg0.writeUTF(uname);
arg0.writeInt(friedsCount);
} @Override
public int compareTo(User o) {
int result = this.uname.compareTo(o.getUname());
if (result == ) {
return Integer.compare(this.friedsCount, o.getFriedsCount());
}
return result;
} }
3, sort
package com.wenbronk.friend; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; /**
* 排序
* @author root
*
*/
public class FofSort extends WritableComparator { public FofSort() {
super(User.class, true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
User user1 = (User) a;
User user2 = (User) b; int compareTo = user1.getUname().compareTo(user2.getUname());
if (compareTo == ) {
compareTo = Integer.compare(user1.getFriedsCount(), user2.getFriedsCount());
}
return compareTo;
} }
4, group
package com.wenbronk.friend; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; /**
* 自定义分组
* @author root
*
*/
public class FofGroup extends WritableComparator { public FofGroup() {
super(User.class, true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
User u1 = (User) a;
User u2 = (User) b;
return u1.getUname().compareTo(u2.getUname());
} }
5, job
package com.wenbronk.friend; import java.io.IOException; import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* 1个mapreduce找到所有的fof关系 第二个mapreduce执行排序
*
* @author root
*/
public class RunJob { public static void main(String[] args) throws IOException {
Configuration configuration = new Configuration();
// configuration.set("mapred.jar", "C:/Users/wenbr/Desktop/fof.jar"); // 本地运行
configuration.set("fs.default", "hdfs://wenbronk.hdfs.com:8020 ");
configuration.set("yarn.resourcemanager", "hdfs://192.168.208.106"); if (runFindFof(configuration)) {
// 根据foffind进行排序
run2(configuration);
} } /**
* 找到所有的fof关系
* @throws IOException
*/
private static boolean runFindFof(Configuration conf) throws IOException {
try {
FileSystem fs = FileSystem.get(conf);
Job job = Job.getInstance(conf);
job.setJobName("friend"); job.setJarByClass(RunJob.class);
job.setMapperClass(FofMapper.class);
job.setReducerClass(FofReduce.class);
job.setMapOutputKeyClass(Fof.class);
job.setMapOutputValueClass(IntWritable.class); // job.setJar("C:/Users/wenbr/Desktop/friend.jar"); job.setInputFormatClass(KeyValueTextInputFormat.class);
// FileInputFormat.addInputPath(job, new Path("/usr/friend.txt"));
FileInputFormat.addInputPath(job, new Path("E:\\sxt\\1-MapReduce\\data\\friend.txt")); Path path = new Path("/root/usr/fof/f1");
if (fs.exists(path)) {
fs.delete(path, true);
}
FileOutputFormat.setOutputPath(job, path);
return job.waitForCompletion(true);
}catch(Exception e) {
e.printStackTrace();
}
return false;
}
static class FofMapper extends Mapper<Text, Text, Fof, IntWritable> {
@Override
protected void map(Text key, Text value, Mapper<Text, Text, Fof, IntWritable>.Context context)
throws IOException, InterruptedException {
// super.map(key, value, context);
String user = key.toString();
String[] frieds = StringUtils.split(value.toString(), '\t'); for (int i = ; i < frieds.length; i++) {
String f1 = frieds[i];
// 去掉是直接好友的, 按组输出, 如果组中有value=0 的, 整组数据舍弃
context.write(new Fof(user, f1), new IntWritable());
for (int j = i + ; j < frieds.length; j++) {
String f2 = frieds[j]; Fof fof = new Fof(f1, f2);
context.write(fof, new IntWritable());
}
}
}
}
static class FofReduce extends Reducer<Fof, IntWritable, Fof, IntWritable> {
@Override
protected void reduce(Fof arg0, Iterable<IntWritable> arg1,
Reducer<Fof, IntWritable, Fof, IntWritable>.Context arg2) throws IOException, InterruptedException {
boolean flag = false;
int sum = ;
for (IntWritable count : arg1) {
// 值有0的, 整组数据舍弃
if (count.get() == ) {
flag = true;
break;
} else {
sum += count.get();
}
} if (!flag) {
arg2.write(arg0, new IntWritable(sum));
}
}
} /**
* 向用户推荐好友
* @param config
*/
public static void run2(Configuration config) {
try {
FileSystem fileSystem = FileSystem.get(config);
Job job = Job.getInstance(config); job.setJobName("fof2"); job.setMapperClass(SortMapper.class);
job.setReducerClass(SortReduce.class);
job.setSortComparatorClass(FofSort.class);
job.setGroupingComparatorClass(FofGroup.class); job.setMapOutputKeyClass(User.class);
job.setMapOutputValueClass(User.class); job.setInputFormatClass(KeyValueTextInputFormat.class); // 设置MR执行的输入文件
FileInputFormat.addInputPath(job, new Path("/usr/output/f1")); // 设置输出文件, 文件不可存在
Path path = new Path("/root/usr/fof/f2");
if (fileSystem.exists(path)) {
fileSystem.delete(path, true);
} FileOutputFormat.setOutputPath(job, path); boolean f = job.waitForCompletion(true);
if (f) {
System.out.println("job, 成功执行");
} }catch (Exception e) {
e.printStackTrace();
}
}
static class SortMapper extends Mapper<Text, Text, User, User> {
@Override
protected void map(Text key, Text value, Mapper<Text, Text, User, User>.Context context)
throws IOException, InterruptedException {
String[] args = StringUtils.split(value.toString(), '\t');
String other = args[];
int friendsCount = Integer.parseInt(args[]);
// 输出两次, 同时给fof两个用户推荐好友
context.write(new User(key.toString(), friendsCount), new User(other, friendsCount));
context.write(new User(other, friendsCount), new User(key.toString(), friendsCount));
}
}
static class SortReduce extends Reducer<User, User, Text, Text>{
@Override
protected void reduce(User arg0, Iterable<User> arg1, Reducer<User, User, Text, Text>.Context arg2)
throws IOException, InterruptedException {
String uname = arg0.getUname();
StringBuilder stringBuilder = new StringBuilder();
for (User user : arg1) {
stringBuilder.append(user.getUname() + ": " + user.getFriedsCount());
stringBuilder.append(", ");
}
arg2.write(new Text(uname), new Text(stringBuilder.toString()));
}
} }
初始文档
小明 老王 如花 林志玲
老王 小明 凤姐
如花 小明 李刚 凤姐
林志玲 小明 李刚 凤姐 郭美美
李刚 如花 凤姐 林志玲
郭美美 凤姐 林志玲
凤姐 如花 老王 林志玲 郭美美
系列来自尚学堂视频
19-hadoop-fof好友推荐的更多相关文章
- 吴裕雄--天生自然HADOOP操作实验学习笔记:qq好友推荐算法
实验目的 初步认识图计算的知识点 复习mapreduce的知识点,复习自定义排序分组的方法 学会设计mapreduce程序解决实际问题 实验原理 QQ好友推荐算法是所有推荐算法中思路最简单的,我们利用 ...
- MapReduce -- 好友推荐
MapReduce实现好友推荐: 张三的好友有王五.小红.赵六; 同样王五.小红.赵六的共同好友是张三; 在王五和小红不认识的前提下,可以通过张三互相认识,给王五推荐的好友为小红, 给小红推荐的好友是 ...
- 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的图书推荐
根据在炼数成金上的学习,将部分代码总结一下在需要的时候可以多加温习.首先根据原理作简要分析.一般推荐系统使用的协同过滤推荐模型:分别是基于ItemCF的推荐模型或者是基于UserCF的推荐模型:首先分 ...
- 19款Windows实用软件推荐,满满的干货,总有一款是你必备的
https://post.smzdm.com/p/745799/ 追加修改(2018-08-20 12:28:23):一些追加内容: 很多人都在吐槽为什么推荐Clover,这里我说明一下,就我了解到的 ...
- 【大数据系列】MapReduce示例好友推荐
package org.slp; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import ...
- 基于hadoop的电影推荐结果可视化
数据可视化 1.数据的分析与统计 使用sql语句进行查询,获取所有数据的概述,包括电影数.电影类别数.人数.职业种类.点评数等. 2.构建数据可视化框架 这里使用了前端框架Bootstrap进行前端的 ...
随机推荐
- MapGIS计算瓦片数据集
https://www.docin.com/p-2103834433.html
- sys/time.h 和 time.h
今天在燕麦工作第二天.看荣哥给我的程序,发现程序里面用的延时跟我以前使用的不同.导入两个头文件,然后用函数来获得时间.关于这个函数特别查来一下. time.h 是ISO C99 标准日期头文件. s ...
- Alpha阶段敏捷冲刺(六)
1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 祁泽文:查找了单词统计的方法. 徐璐琳:通过" ...
- hdu 2642 Stars 【二维树状数组】
题目 题目大意:Yifenfei是一个浪漫的人,他喜欢数天上的星星.为了使问题变得更容易,我们假设天空是一个二维平面,上面的星星有时会亮,有时会发暗.最开始,没有明亮的星星在天空中,然后将给出一些信息 ...
- Android创建AVD模拟器
Android创建AVD模拟器 1. 新建一个安卓工程如下: 2. 点击windows里的 Android Virtual Device Manager,如果第一次安装 adt-bundle启动会出现 ...
- noah
1.url:controller/method 2.在index.php中设置display_errors:1 能看到错误提示
- 在推送提交之后阻止Azure DevOps (TFS)持续集成
在Azure DevOps服务器上配置生成定义时,可以配置连续集成(CI)生成.每次签入或提交到源代码库时都会自动运行一个CI构建.这种机制允许开发人员启动一个自动化的过程,例如编译和部署构建.这是一 ...
- SQL SERVER的锁机制(一)——概述(锁的种类与范围)
锁定:通俗的讲就是加锁.锁定是 Microsoft SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 定义:当有事务操作时,数据库引擎会要求不同类型的锁定,如相关数 ...
- docker容器间跨主机通信
http://jnzg905.iteye.com/blog/2269583 https://blog.csdn.net/pingpangbing0902/article/details/7823889 ...
- 25_re模块
一.re模块的核心功能 1.findall —— 查找所有,返回list lst = re.findall("m", "mai le fo len, mai ...