Hadoop实战训练————MapReduce实现PageRank算法
经过一段时间的学习,对于Hadoop有了一些了解,于是决定用MapReduce实现PageRank算法,以下简称PR
先简单介绍一下PR算法(摘自百度百科:https://baike.baidu.com/item/google%20pagerank/2465380?fr=aladdin&fromid=111004&fromtitle=pagerank):
2005年初,Google为网页链接推出一项新属性nofollow,使得网站管理员和网站作者可以做出一些Google不计票的链接,也就是说这些链接不算作"投票"。nofollow的设置可以抵制评论垃圾。



1、每个页面的PR值和上一次计算的PR相等
2、设定一个差值指标(0.0001)。当所有页面和上一次计算的PR差值平均小于该标准时,则收敛。
3、设定一个百分比(99%),当99%的页面和上一次计算的PR相等
本文将采用第二种方式来实现该算法:

A B D
B C
C A B
D B C
其中每一行的后面的页面为第一个页面的出链(A可以链到B和C)
由于需要统计每个页面的入链页面和出链数,因此需要两个MapReduce,第一个用于统计入链和出链,第二个用于循环统计PR值,代码如下:
package com.tyx.mapreduce.PageRank; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by tyx on 2017/11/29.
*/
public class RunPageRankJob { // 统计所有链接的入链
private static Map<String,String > allInLine = new HashMap<>();
// 统计所有链接的出链
private static Map<String,Integer> allOutLine = new HashMap<>();
// 统计所有链接的现有pagerank
private static Map<String,Double> allPageRank = new HashMap<>();
// 统计所有链接计算后的pagerank
private static Map<String ,Double> allNextPageRank = new HashMap<>(); public static void main(String[] args) {
Configuration configuration = new Configuration();
// configuration.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR," ");
configuration.set("fs.defaultFS", "hdfs://node1:8020");
configuration.set("yarn.resourcemanager.hostname", "node1");
// 第一个MapReduce为了统计出每个页面的入链,和每个页面的出链数
if (run1(configuration)){
run2(configuration);
}
} /*
输入数据:
A B D
B C
C A B
D B C*/ static class AcountOutMapper extends Mapper<Text,Text,Text,Text>{
@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
// super.map(key, value, context);
int num = 0;
// 若A能连接到B,则说明B是A的一条出链
String[] outLines = value.toString().split("\t");
for (int i=0;i<outLines.length;i++){
context.write(new Text(outLines[i]),key);
}
num = outLines.length;
// 统计出链
context.write(key,new Text("--"+num));
}
} static class AcountOutReducer extends Reducer<Text,Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
// super.reduce(key, values, context);
// 统计该页面的入链页面
String outStr = "";
int sum = 0;
for (Text text : values){
// 统计出链数目
if (text.toString().contains("--")){
sum += Integer.parseInt(text.toString().replaceAll("--",""));
}else {
outStr += text+"\t";
}
}
context.write(key,new Text(outStr+sum));
allOutLine.put(key.toString(),sum);
allInLine.put(key.toString(),outStr);
allPageRank.put(key.toString(),1.0);
}
} public static boolean run1(Configuration configuration){
try {
Job job = Job.getInstance(configuration);
FileSystem fileSystem = FileSystem.get(configuration); job.setJobName("acountline"); job.setJarByClass(RunPageRankJob.class); job.setMapperClass(AcountOutMapper.class);
job.setReducerClass(AcountOutReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setInputFormatClass(KeyValueTextInputFormat.class); Path intPath = new Path("/usr/output/pagerank.txt");
FileInputFormat.addInputPath(job,intPath); Path outPath = new Path("/usr/output/acoutline");
if (fileSystem.exists(outPath)){
fileSystem.delete(outPath,true);
}
FileOutputFormat.setOutputPath(job,outPath); boolean f = job.waitForCompletion(true);
return f;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} /*第一次MapReduce输出数据:
A C
B A C D
C B D
D A*/ static class PageRankMapper extends Mapper<Text,Text,Text,Text>{
@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
// super.map(key, value, context);
String myUrl = key.toString();
// 取出该页面所有的入链页面
String inLines = allInLine.get(myUrl);
context.write(key,new Text(inLines));
}
} static class PageRankReducer extends Reducer<Text,Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
// super.reduce(key, values, context);
// 后半段求和公式的和 (PR(1)/L(1)…………PR(i)/L(i)
double sum = 0.0;
String outStr = "";
for (Text text : values){
String[] arr = text.toString().split("\t");
for (int i=0;i<arr.length;i++){
outStr += arr[i]+"\t";
sum += allPageRank.get(arr[i])/allOutLine.get(arr[i]);
}
}
// 算出该页面本次的PR结果
double nowPr = (1-0.85)/allPageRank.size()+0.85*sum; allNextPageRank.put(key.toString(),nowPr);
context.write(key,new Text(outStr));
}
} public static void run2(Configuration configuration){
double d = 0.001;
int i=1;
// 迭代循环趋于收敛
while (true){
try {
configuration.setInt("count",i);
i++;
Job job = Job.getInstance(configuration);
FileSystem fileSystem = FileSystem.get(configuration); job.setJobName("pagerank");
job.setJarByClass(RunPageRankJob.class);
job.setJobName("Pr"+i); job.setMapperClass(PageRankMapper.class);
job.setReducerClass(PageRankReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setInputFormatClass(KeyValueTextInputFormat.class); Path intPath = new Path("/usr/output/pagerank.txt");
if (i>2){
intPath = new Path("/usr/output/Pr"+(i-1));
}
FileInputFormat.addInputPath(job,intPath); Path outPath = new Path("/usr/output/Pr"+i);
if (fileSystem.exists(outPath)){
fileSystem.delete(outPath,true);
}
FileOutputFormat.setOutputPath(job,outPath); boolean f = job.waitForCompletion(true);
if (f){
System.out.println("job执行完毕");
double sum = 0.0;
// 提取本轮所有页面的PR值和上一轮作比较,
for (String key : allPageRank.keySet()){
System.out.println(key+"--------------------------"+allPageRank.get(key));
sum += Math.abs(allNextPageRank.get(key)-allPageRank.get(key));
allPageRank.put(key,allNextPageRank.get(key));
}
System.out.println(sum);
// 若平均差小于d则表示收敛完毕
if (sum/allPageRank.size()<d){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
最终结果输入如下:

由图可知在这四个页面组成的互联网集群中,页面C的重要性是最高的
本次操作一共经过了30次循环:

若有不对之处请不吝指教,谢谢
转载请注明出处http://www.cnblogs.com/liuxiaopang/p/7930508.html
Hadoop实战训练————MapReduce实现PageRank算法的更多相关文章
- MapReduce实现PageRank算法(邻接矩阵法)
前言 之前写过稀疏图的实现方法,这次写用矩阵存储数据的算法实现,只要会矩阵相乘的话,实现这个就很简单了.如果有不懂的可以先看一下下面两篇随笔. MapReduce实现PageRank算法(稀疏图法) ...
- MapReduce实现PageRank算法(稀疏图法)
前言 本文用Python编写代码,并通过hadoop streaming框架运行. 算法思想 下图是一个网络: 考虑转移矩阵是一个很多的稀疏矩阵,我们可以用稀疏矩阵的形式表示,我们把web图中的每一个 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:pagerank算法
实验目的 了解PageRank算法 学会用mapreduce解决实际的复杂计算问题 实验原理 1.pagerank算法简介 PageRank,即网页排名,又称网页级别.Google左侧排名或佩奇排名. ...
- Hadoop实战5:MapReduce编程-WordCount统计单词个数-eclipse-java-windows环境
Hadoop研发在java环境的拓展 一 背景 由于一直使用hadoop streaming形式编写mapreduce程序,所以目前的hadoop程序局限于python语言.下面为了拓展java语言研 ...
- Hadoop实战3:MapReduce编程-WordCount统计单词个数-eclipse-java-ubuntu环境
之前习惯用hadoop streaming环境编写python程序,下面总结编辑java的eclipse环境配置总结,及一个WordCount例子运行. 一 下载eclipse安装包及hadoop插件 ...
- Hadoop实战2:MapReduce编程-WordCount实例-streaming-python环境
这是搭建hadoop环境后的第一个MapReduce程序: 基于hadoop streaming的python的脚本: 1 map.py文件,把文本的内容划分成单词: #!/usr/bin/pytho ...
- 升级版:深入浅出Hadoop实战开发(云存储、MapReduce、HBase实战微博、Hive应用、Storm应用)
Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.Hadoop实现了一个分布式文件系 ...
- Hadoop应用开发实战(flume应用开发、搜索引擎算法、Pipes、集群、PageRank算法)
Hadoop是2013年最热门的技术之一,通过北风网robby老师<深入浅出Hadoop实战开发>.<Hadoop应用开发实战>两套课程的学习,普通Java开发人员可以在最快的 ...
- 王家林的“云计算分布式大数据Hadoop实战高手之路---从零开始”的第十一讲Hadoop图文训练课程:MapReduce的原理机制和流程图剖析
这一讲我们主要剖析MapReduce的原理机制和流程. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发 ...
随机推荐
- Android基础知识03—Activity的基本用法
------Activity 活动------ 活动 Activity 是一种包含用户界面的组件,即一个界面就是一个活动 创建活动的过程: >> 创建一个类,继承自Activity类,并且 ...
- 深入理解final和static关键字
深入理解final和static关键字 参考:http://blog.csdn.net/qq1028951741/article/details/53418852 final关键字 final关键字可 ...
- Java基础总结--IO总结2
1.键盘录入--Java具有特定的对象封装这些输入输出设备在System类定义 in-InputStream类型和out-PrintStream类型成员变量阻塞是方法:read()无数据就阻塞wind ...
- 微信小程序之页面跳转路径问题
错误如下: 业务需求:在movie页面点击进入detail页面. 在遍历跳转路径的时候,写绝对路径了 只需改一下就好了 教程参考地址:http://blog.csdn.net/reylen/artic ...
- HDU 4267 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- AngularJS学习篇(八)
AngularJS 服务(Service) 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. AngularJS 内建了30 多个服务. 为什么使用服务? ...
- struts2框架的登录制作
首先:我们要建一个web项目 接着: 我们先来导入struts的xml文件 第一步:右击你的项目名,鼠标到MyEclipse会看到一个add struts开头的文件,点开以后看到: 这里我们选择str ...
- linux-more
more 这个命令可以用来分页查看大篇幅的文件内容非常有效 命令参数: -num : 这里的num 是一个数字,用来指定分页显示时每页的行数 +num : 指定从文件的第几行num开始显示 ... ...
- 块级元素行内元素以及display属性
1.什么叫做标签语义化? ->合理的标签做合适的事情 ->HTML中常用的标签都有哪些? (块状标签和行内标签) ->块状标签和行内标签的区别? (常用的有8条区别) 1)内联元素: ...
- javascript 二维(多维)数组的复制问题
最近在项目中遇到一个动画暂停的效果,需要在动画停止的时候检测当前坐标和已经运行的时间,从而调节时间轴为再次运行时加速. 但是在数组保存方面折腾了半天. var orbitArray = [], lin ...