好友推荐的案例, 需要两个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好友推荐的更多相关文章

  1. 吴裕雄--天生自然HADOOP操作实验学习笔记:qq好友推荐算法

    实验目的 初步认识图计算的知识点 复习mapreduce的知识点,复习自定义排序分组的方法 学会设计mapreduce程序解决实际问题 实验原理 QQ好友推荐算法是所有推荐算法中思路最简单的,我们利用 ...

  2. MapReduce -- 好友推荐

    MapReduce实现好友推荐: 张三的好友有王五.小红.赵六; 同样王五.小红.赵六的共同好友是张三; 在王五和小红不认识的前提下,可以通过张三互相认识,给王五推荐的好友为小红, 给小红推荐的好友是 ...

  3. MapReduce案例-好友推荐

    用过各种社交平台(如QQ.微博.朋友网等等)的小伙伴应该都知道有一个叫 "可能认识" 或者 "好友推荐" 的功能(如下图).它的算法主要是根据你们之间的共同好友 ...

  4. 【Hadoop学习之十】MapReduce案例分析二-好友推荐

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...

  5. MapReduce案例二:好友推荐

    1.需求 推荐好友的好友 图1: 2.解决思路 3.代码 3.1MyFoF类代码 说明: 该类定义了所加载的配置,以及执行的map,reduce程序所需要加载运行的类 package com.hado ...

  6. 基于hadoop的图书推荐

    根据在炼数成金上的学习,将部分代码总结一下在需要的时候可以多加温习.首先根据原理作简要分析.一般推荐系统使用的协同过滤推荐模型:分别是基于ItemCF的推荐模型或者是基于UserCF的推荐模型:首先分 ...

  7. 19款Windows实用软件推荐,满满的干货,总有一款是你必备的

    https://post.smzdm.com/p/745799/ 追加修改(2018-08-20 12:28:23):一些追加内容: 很多人都在吐槽为什么推荐Clover,这里我说明一下,就我了解到的 ...

  8. 【大数据系列】MapReduce示例好友推荐

    package org.slp; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import ...

  9. 基于hadoop的电影推荐结果可视化

    数据可视化 1.数据的分析与统计 使用sql语句进行查询,获取所有数据的概述,包括电影数.电影类别数.人数.职业种类.点评数等. 2.构建数据可视化框架 这里使用了前端框架Bootstrap进行前端的 ...

随机推荐

  1. 创建私有maven服务器

    私服的创建 1.下载nexus服务  nexus-2.12.0-01-bundle https://pan.baidu.com/s/1o8OfieI 2.下载maven工具   apache-mave ...

  2. 20155326 2016-2017-2 《Java程序设计》第6周学习总结

    20155326 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 InputStream与OutputStream: 串流设计的概念 (1)Java将输入/输 ...

  3. noip第4课资料

  4. noip第29课作业

    1.   钢条切割 [问题描述] 一家公司购买长钢条,将其切割成短钢条出售,切割本身没有成本,长度为i的短钢条的价格为Pi.那给定一段长度为n的钢条和一个价格表Pi,求钢条的切割方案使得收益Rn最大. ...

  5. Docker windows下安装并搭建Nodejs的webapp

    一.关于Docker 什么是Docker?Docker 采用go语言编写,是一个开源的应用容器引擎.让开发者可以快速打包他们的应用以及依赖包到一个封装的可移植的容器Image中,然后发布到任何流行的机 ...

  6. Java 理论与实践: 用弱引用堵住内存泄漏

    弱引用使得表达对象生命周期关系变得容易了 虽然用 Java™ 语言编写的程序在理论上是不会出现“内存泄漏”的,但是有时对象在不再作为程序的逻辑状态的一部分之后仍然不被垃圾收集.本月,负责保障应用程序健 ...

  7. django创建分页

    前台html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. hdu 1.3.3 今年暑假不AC

    //简单.... 1 #include<iostream> #include<cstdio> #include<cstdlib> using namespace s ...

  9. SQL SERVER的锁机制(一)——概述(锁的种类与范围)

    锁定:通俗的讲就是加锁.锁定是 Microsoft SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 定义:当有事务操作时,数据库引擎会要求不同类型的锁定,如相关数 ...

  10. TwoSum / Three Sum

    Let's begin with a naive method. We first need to sort the array A[n]. And we want to solve the prob ...