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进行前端的 ...
随机推荐
- Surface 2装机必备软件指南
新买的Surface到货了还不知道有什么用,每天就用来划划点点?有点太浪费了吧!跟哥走,哥给你推荐几款Surface 2装机必备的软件~应用商店,走起~ 初次使用看过来:Win8宝典 如果你是一个像我 ...
- Maven中的配置文件
<span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...
- [mysql] mysql 查询语句收集
// 1. 筛选出当天的中奖名单 $where = " date_format(f_ctime,'%Y-%m-%d') = current_date()"; ...
- Oracle PLSQL读取(解析)Excel文档
http://www.itpub.net/thread-1921612-1-1.html !!!https://code.google.com/p/plsql-utils/ Introduction介 ...
- NW.js安装原生node模块node-printer控制打印机
1.安装原生node模块 #全局安装nw-gyp npm install -g nw-gyp #设置目标NW.js版本 set npm_config_target=0.31.4 #设置构建架构,ia3 ...
- scrapy中Request中常用参数
url: 就是需要请求,并进行下一步处理的url callback: 指定该请求返回的Response,由那个函数来处理. method: 一般不需要指定,使用默认GET方法请求即可 headers: ...
- 【Win】Clso QR Tool 二维码小工具
一个可以生成并识别二维码的windows小工具,纯绿色.不含糖. 可以通过输入文本生成二维码,或者加载本地图片.剪贴板内的图片,直接解析出二维码内容. 支持自定义LOGO. 下载文件 (当前版本:1. ...
- 自定义WPF窗体形状
介绍 你好WPF爱好者. 随着WPF等统一API语言的发明,丰富用户界面变得非常容易. 创建丰富的用户界面只是一个想法. 您需要拥有的是创造性思维和最新技术融合. WPF和Expression Ble ...
- 9.翻译:EF基础系列---使用EF开发的方式有哪些?
原文链接:http://www.entityframeworktutorial.net/choosing-development-approach-with-entity-framework.aspx ...
- Token的生成和检验
package TestToken; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt. ...