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进行前端的 ...
随机推荐
- 泡泡机器人SLAM 2019
LDSO:具有回环检测的直接稀疏里程计:LDSO:Direct Sparse Odometry with Loop Closure Abstract—In this paper we present ...
- day19(乱码解决方案)
乱码的解决方案: 首先需要理解乱码的产生原因:无非就是编码格式的问题 1.服务器到浏览器 字节流传输时 产生乱码的原因: 1.浏览器打开方式不对(打开时 的方式为默认的方式,各个浏览器的方式都不同) ...
- [转]Windows中使用命令行方式编译打包Android项目
http://my.oschina.net/liux/blog/37875 网上很多用Ant来编译打包Android应用的文章,毕竟Ant是纯Java语言编写的,具有很好的跨平台性.今天想写个纯win ...
- python常用算法题
一: #计算平均数,中位数和众数 #coding:utf-8 #计算平均数,中位数和众数 import random lst = [random.randint(0,10) for i in rang ...
- Amoeba变形虫
我们通过路由选择来决定操作时访问那个数据库,而路由的选择方式不外乎以下几种: 1) SpringAOP方式:spring底层配置多个数据源,配置路由(面向切面编程)手工写很多代码(废除) 2) MyS ...
- Delphi-idHttp-Post JSON用法 good
从国外网站抄来的代码 Delphi source: http := TIdHttp.Create(nil);http.HandleRedirects := True;//允许头转向http.ReadT ...
- asp.net core mvc 中间件之路由
asp.net core mvc 中间件之路由 路由中间件 首先看路由中间件的源码 先用httpContext实例化一个路由上下文,然后把中间件接收到的路由添加到路由上下文的路由集合 然后把路由上下文 ...
- ASP.NET MVC5 高级编程-学习日记-第二章 控制器
2.1 控制器的角色 MVC模式中的控制器(Controller)主要负责响应用户的输入,冰球在响应时修改模型(Model).通过这种方式,MVC模式中的控制器主要关注的是应用程序流.输入数据的处理, ...
- Winform文件上传
近期在做了一个winform的项目的附件上传的需求 最初项目选型的时候有三种 1.使用webservice.webapi上传文件 2,直接保存在数据库中 3.使用共享目录+dos命令 第一种有文件大小 ...
- Duolingo 提高用户留存率的6个手段
翻译 :马玉洁 欢迎访问网易云社区,了解更多网易技术产品运营经验. 如果你用过"Duolingo"(Duolingo)这个语言教育应用程序,你就会知道它就像一款游戏. 这当然不是巧 ...