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求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
随机推荐
- Java——常用类(基础类型数据包装类)
[包装类] 包装类(如Integer.Double等)这些类封装了一个相应的基础数据类型数值,并为其提供了一系列操作. 例如:java.lang.Integer类提供了以下构造方法: ...
- 洛谷p3955 图书管理员(NOIP2017 t2)
蒟蒻的最后一篇pas题解...目前转c++ ing 回顾了一下,发现c++的string真的好繁啊(主要我这个蒟蒻太菜不会用) 还是pas的string操作简洁 做法 我这种蒟蒻不像别的dalao,懒 ...
- jsoncpp 能做什么
jsoncpp能做什么1)跨平台跨语言动态信息数据交换.2)作为格式化配置文件使用3)对应数据结构数据类型做序列化和反序列化4)value::toStyledString 格式化json串输出 一.w ...
- (10)python学习笔记一
学习参考博客:http://blog.csdn.net/a359680405/article/details/42486689 深表感谢 1.单行注释 # 多行注释 "" ...
- token与安全
http://ju.outofmemory.cn/entry/134189 关于 Token,你应该知道的十件事 https://blog.csdn.net/Fabulous1111/art ...
- [洛谷P3939]:数颜色(二分)
题目传送门 题目描述 小$C$的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有相同的颜色.小$C$把她标号从$1$到$n$的$n$只兔子排成长长的一排,来给他们喂胡萝卜吃.排列 ...
- influxDB 1.3 中文文档
influxDB是一个旨在处理高并发写入和查询负载的时序数据库,它是TICK框架的第二部分,influxdb用于任何包含大量时序数据应用的后台存储,包括Devops监控.应用指标数据.物联网传感器数据 ...
- jquery.ui.widget.js
- 图像bayer格式介绍【转】
本文转载自:http://www.cnblogs.com/whw19818/p/6223143.html 1 图像bayer格式介绍 bayer格式图片是伊士曼·柯达公司科学家Bryce Bayer发 ...
- __main__ — Top-level script environment
w 29.4. __main__ — Top-level script environment — Python 3.6.1 documentation https://docs.python.or ...