一、实验目的
共同好友推荐算法,统计出两个人共同好友的多少,决定这两个人关系的强弱,基比较并评估网页优化的成效。了解pagerank算法的思想和原理。了解链式结构的应用和意义。
二、实验要求
1.于共同好友的多少,就可以进行好友推荐
2.PageRank算法,对每个目标网页进行附上权值,权值大的就靠前显示,权值小的就靠后显示。
三、实验运行结果
四、实验代码
FMapper:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;
public class FMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
Text tkey =new Text();
IntWritable tval=new IntWritable();
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] words=StringUtils.split(value.toString(),’ ');
for(int i=1;i < words.length;i++){
tkey.set(getFd(words[0],words[i]));
tval.set(0);
context.write(tkey, tval);
for(int j = i+1;j < words.length;j++ ){
tkey.set(getFd(words[i],words[j]));
tval.set(1);
context.write(tkey, tval);
}
}
}
private String getFd(String a, String b) {
return a.compareTo(b)>0?b+":"+a:a+":"+b;
}
}
Myfd:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class Myfd {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf =new Configuration(true);
Job job = Job.getInstance(conf);