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求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
随机推荐
- Flink Batch SQL 1.10 实践
Flink作为流批统一的计算框架,在1.10中完成了大量batch相关的增强与改进.1.10可以说是第一个成熟的生产可用的Flink Batch SQL版本,它一扫之前Dataset的羸弱,从功能和性 ...
- 7,HashMap
一,HashMap简介 1,HashMap 是一个散列表,它存储的内容是键值对(key-value)映射. 2,HashMap 继承于AbstractMap,实现了Map.Cloneable.java ...
- JavaScript method overload
https://stackoverflow.com/questions/2187666/help-with-js-and-functions-parameters JavaScript doesn' ...
- 【后台管理系统】—— Ant Design Pro组件使用(一)
一.搜索Search 搜索框 <Search placeholder="请输入关键字" defaultValue={kw && kw != 'nul ...
- optistruct线性求解一次二次单元应力位移比较
通过分析比较10mm.5mm.3mm.1mm的网格模型, 网格越细密: 位移与应力均趋于恒定值(收敛): 一次与二次单元的应力区域一致: 一次与二次单元的位移相差11.3%,一次单元的位移小. 所用的 ...
- find查找特殊权限用法
find查找特殊权限的用法 find選項與參數: 3. 與檔案權限及名稱有關的參數: -name filename:搜尋檔案名稱為 filename 的檔案: -size [+-]SIZE:搜尋比 S ...
- 测开之路八十六:python操作sqlite
创建sqlite数据库,并创建表和数据 python自带sqlite3库可以创建数据库文件 导入库:import sqlite3 创建游标,指定数据库名字:con = sqlite3.connect( ...
- jmeter之分布式压测
很多性能大牛说一台机器的压测其实不准确,于是搜索网上的分布式压测练习了一番 目录 1.环境准备 2.控制机和压测机配置 3.执行分布式压测 1.环境准备 1.1准备一台windows作为控制机(mas ...
- 使用pyautogui替代selenium,图像识别进行web自动化测试--基于python语言
这里演示一下使用pyautogui替代selenium进行web自动化测试,并不建议使用pyautogui进行web自动化测试,因为元素的ui一旦有长宽变化,或者风格的变化,执行时就会发生异常,仅当学 ...
- A heavy dew refreshed the earth at night
brightness.n.明亮 endless.adj.无止境的 degradation.n.堕落 superiority.n.优越性 valuable.adj.有价值的 flake.n.小薄片 cu ...