Hadoop MapReduce实现人员二度关系运算
1、一度人脉:双方直接是好友
2、二度人脉:双方有一个以上共同的好友,这时朋友网可以计算出你们有几个共同的好友并且呈现数字给你。你们的关系是: 你->朋友->陌生人
3、三度人脉:即你朋友的朋友的朋友就是这个陌生人。你们的关系是 你->朋友->朋友->陌生人
4、四度人脉:比三度增加一度,你们的关系是,你->朋友->朋友->朋友->陌生人
5、五度人脉:你->朋友->朋友->朋友->朋友->陌生人 ,像上面这张图片表示的就是一个五度人脉关系。
6、六度人脉:你->朋友->朋友->朋友->朋友->朋友->陌生人
求下图的二度人员关系:
数据格式如下:
A,B
A,C
A,E
B,D
E,D
C,F
F,G
实现如下:
package com.gm.hadoop.mapreduce;
import java.io.IOException;
import java.util.Vector;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class App_2_Friend {
// map1
public static class Map1 extends Mapper<Object, Text, Text, Text> {
private Text map1_key = new Text();
private Text map1_value = new Text();
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String[] eachterm = value.toString().split(",");
if (eachterm.length != 2) {
return;
}
if (eachterm[0].compareTo(eachterm[1]) < 0) {
map1_value.set(eachterm[0] + "\t" + eachterm[1]);
} else if (eachterm[0].compareTo(eachterm[1]) > 0) {
map1_value.set(eachterm[1] + "\t" + eachterm[0]);
}
map1_key.set(eachterm[0]);
context.write(map1_key, map1_value);
map1_key.set(eachterm[1]);
context.write(map1_key, map1_value);
}
}
// reduce1
public static class Reduce1 extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Vector<String> hisFriends = new Vector<String>();
for (Text val : values) {
String[] eachterm = val.toString().split("\t");
if (eachterm[0].equals(key.toString())) {
hisFriends.add(eachterm[1]);
context.write(val, new Text("deg1friend"));
}
if (eachterm[1].equals(key.toString())) {
hisFriends.add(eachterm[0]);
context.write(val, new Text("deg1friend"));
}
}
for (int i = 0; i < hisFriends.size(); i++) {
for (int j = 0; j < hisFriends.size(); j++) {
if (hisFriends.elementAt(i).compareTo(
hisFriends.elementAt(j)) < 0) {
Text reduce_key = new Text(hisFriends.elementAt(i)
+ "\t" + hisFriends.elementAt(j));
context.write(reduce_key, new Text("deg2friend"));
}
}
}
}
}
// map2
public static class Map2 extends Mapper<Object, Text, Text, Text> {
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String[] line = value.toString().split("\t");
if (line.length == 3) {
Text map2_key = new Text(line[0] + "\t" + line[1]);
Text map2_value = new Text(line[2]);
context.write(map2_key, map2_value);
}
}
}
// reduce2
public static class Reduce2 extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
boolean isdeg1 = false;
boolean isdeg2 = false;
int count = 0;
for (Text val : values) {
if (val.toString().compareTo("deg1friend") == 0) {
isdeg1 = true;
}
if (val.toString().compareTo("deg2friend") == 0) {
isdeg2 = true;
count++;
}
}
if ((!isdeg1) && isdeg2) {
context.write(new Text(String.valueOf(count)), key);
}
}
}
// main
public static void main(String[] args) throws Exception {
System.setProperty("hadoop.home.dir", "E:/hadoop-2.8.0");
//HADOOP_HOME and hadoop.home.dir are unset.
Configuration conf = new Configuration();
/*String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();*/
/*if (otherArgs.length != 3) {
System.err.println("Usage: Deg2friend <in> <temp> <out>");
System.exit(2);
}*/
String in_path = "C:/rmgx.txt";
String temp_path = "C:/temp.txt";
String out_path = "C:/result.txt";
Job job1 = Job.getInstance(conf, "Deg2friend");
job1.setJarByClass(App_2_Friend.class);
job1.setMapperClass(Map1.class);
job1.setReducerClass(Reduce1.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);
/*FileInputFormat.addInputPath(job1, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job1, new Path(otherArgs[1]));*/
FileInputFormat.addInputPath(job1, new Path(in_path));
FileOutputFormat.setOutputPath(job1, new Path(temp_path));
if (job1.waitForCompletion(true)) {
Job job2 = Job.getInstance(conf, "Deg2friend");
job2.setJarByClass(App_2_Friend.class);
job2.setMapperClass(Map2.class);
job2.setReducerClass(Reduce2.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
/*FileInputFormat.addInputPath(job2, new Path(otherArgs[1]));
FileOutputFormat.setOutputPath(job2, new Path(otherArgs[2]));*/
FileInputFormat.addInputPath(job2, new Path(temp_path));
FileOutputFormat.setOutputPath(job2, new Path(out_path));
System.exit(job2.waitForCompletion(true) ? 0 : 1);
}
System.exit(job1.waitForCompletion(true) ? 0 : 1);
}
}
Hadoop MapReduce实现人员二度关系运算的更多相关文章
- Spark 计算人员二度关系
1.一度人脉:双方直接是好友 2.二度人脉:双方有一个以上共同的好友,这时朋友网可以计算出你们有几个共同的好友并且呈现数字给你.你们的关系是: 你->朋友->陌生人 3.三度人脉:即你朋友 ...
- 基于Spark GraphX计算二度关系
关系计算问题描述 二度关系是指用户与用户通过关注者为桥梁发现到的关注者之间的关系.目前微博通过二度关系实现了潜在用户的推荐.用户的一度关系包含了关注.好友两种类型,二度关系则得到关注的关注.关注的好友 ...
- Spark 计算人员三度关系
1.一度人脉:双方直接是好友 2.二度人脉:双方有一个以上共同的好友,这时朋友网可以计算出你们有几个共同的好友并且呈现数字给你.你们的关系是: 你->朋友->陌生人 3.三度人脉:即你朋友 ...
- Hadoop Mapreduce 参数 (二)
MergeManagerImpl 类 内存参数计算 maxInMemCopyUse 位于构造函数中 final float maxInMemCopyUse = jobConf.getFloat(MRJ ...
- 海量数据的二度人脉挖掘算法(Hadoop 实现)
最近做了一个项目,要求找出二度人脉的一些关系,就好似新浪微博的“你可能感兴趣的人” 中,间接关注推荐:简单描述:即你关注的人中有N个人同时都关注了 XXX . 在程序的实现上,其实我们要找的是:若 U ...
- MapReduce实现二度好友关系
一.问题定义 我在网上找了些,关于二度人脉算法的实现,大部分无非是通过广度搜索算法来查找,犹豫深度已经明确了2以内:这个算法其实很简单,第一步找到你关注的人:第二步找到这些人关注的人,最后找出第二步结 ...
- hadoop计算二度人脉关系推荐好友
https://www.jianshu.com/p/8707cd015ba1 问题描述: 以下是qq好友关系,进行好友推荐,比如:老王和二狗是好友 , 二狗和春子以及花朵是好友,那么老王和花朵 或者老 ...
- 使用MapReduce实现二度人脉搜索算法
一,背景介绍 在新浪微博.人人网等社交网站上,为了使用户在网络上认识更多的朋友,社交网站往往提供类似“你可能感兴趣的人”.“间接关注推荐”等好友推荐的功能,其中就包含了二度人脉算法. 二,算法实现 原 ...
- Hadoop阅读笔记(二)——利用MapReduce求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
随机推荐
- Vue框架基础概要
Vue.js是什么? Vue.js(读音 /vjuː/,类似于 view 的读音)是一套构建用户界面(user interface)的渐进式框架.与其他重量级框架不同的是,Vue 从根本上采用最小成本 ...
- Java——类的继承、访问控制
[继承] <1>Java只支持单继承,不支持多继承. <2>继承父类的私有成员变量,只有所有权,没有使用权. [继承中的构造方法]
- UNR#3 Day1——[ 堆+ST表+复杂度分析 ][ 结论 ][ 线段树合并 ]
地址:http://uoj.ac/contest/45 第一题是鸽子固定器. 只会10分.按 s 从小到大排序,然后 dp[ i ][ j ][ k ] 表示前 i 个元素.已经选了 j 个.最小值所 ...
- 常见的七种Hadoop和Spark项目案例
常见的七种Hadoop和Spark项目案例 有一句古老的格言是这样说的,如果你向某人提供你的全部支持和金融支持去做一些不同的和创新的事情,他们最终却会做别人正在做的事情.如比较火爆的Hadoop.Sp ...
- 【洛谷P1983 车站分级】
这题好像是个蓝题.(不过也确实差不多QwQ)用到了拓扑排序的知识 我们看这些这车站,沿途停过的车站一定比未停的车站的级别高 所以,未停靠的车站向已经停靠的车站连一条边,入度为0的车站级别就看做1 然后 ...
- UITextView输入中文时限制字数
最近因为项目中输入用户简称时限制长度,被测试部多次提交问题,最终将解决方法分享给大家. 刚开始用最简单的方法,在textView的代理方法里面做处理 - (void)textViewDidChange ...
- Mac开机自动运行shell脚本
1.首先写一个sh脚本,比如: cd ~/Documents mkdir haha 代码很简单,进入Documents文件夹,建立haha目录,保存为run.sh 2.修改run.sh权限 sudo ...
- MySQL的常用JSON函数
1. JSON_SEARCH(col ->> '$[*].key', type, val) col: JSON格式的字段名 key:要搜索的col字段的key type:可以为'one'或 ...
- dependencies 和 starter
以 spring-cloud-alibaba-dependencies-1.5.0.RELEASE 为例: <dependency> <groupId>com.alibaba. ...
- JS手写状态管理的实现(转)
https://juejin.im/post/5c528411e51d456898361e43