0


MapReduce课程设计 好友推荐功能

1.实例介绍

  好友推荐算法在实际的社交环境中应用较多,比如qq软件中的“你可能认识的好友° 或者Facebook中的好友推介。好友推荐功能简单的说是这样一个需求,预测菜两个人是否认识,并推荐为好友,并且某两个非好友的用户,他们的共同好友越多。那么他们越可能认识。

2.项目说明

  • 互为推荐关系 - 非好友的两个人之间存在相同好友则互为推荐关系- 朋友圈两个非好友的人,存在共同好友人数越多,越值得推荐- 存在一个共同好友,值为1;存在多个值累加

3.数据

xiaoming laowang renhua linzhiling
laowang xiaoming fengjie
renhua xiaoming ligang fengjie
linzhiling xiaoming ligang fengjie guomeimei
ligang renhua fengjie linzhiling
guomeimei fengjie linzhiling
fengjie renhua laowang linzhiling guomeimei

数据使用空格分割,每行是一个用户以及其对应的好友,每行的第一列名字是用户的名字,后面的是其对应的好友。

4.程序实现

4.1在pom.xml中添加依赖

<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-jobclient</artifactId> <version>2.9.2</version> <scope>provided</scope> </dependency> </dependencies>

** 4.2程序代码**

FriendsRecommendMapper

public class FriendsRecommendMapper extends Mapper<Object, Text,Text, IntWritable> {
    Text ikey = new Text();
    IntWritable eval = new IntWritable();
    public void map(Object key, Text value, Mapper.Context context) throws IOException, InterruptedException{
        String[] line = value.toString().split(" ");
        for (int i = 0; i < line.length; i++) {
            ikey.set(getF(line[0],line[i]));
            eval.set(0);
            context.write(ikey, eval);
            for (int j = i+1; j <line.length ; j++) {
                ikey.set(getF(line[i],line[j]));
                eval.set(1);
                context.write(ikey, eval);
            }
        }
    }
    public String getF(String s1,String s2){
        if(s1.compareTo(s2)<0){
            return s1+":"+s2;
        }
        else {
            return s2+":"+s1;
        }
    }
}

FriendsRecommendReduce

public class FriendsRecommendReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        int flg = 0;
        int sum  = 0;
        for (IntWritable val : values) {
            if(val.get()==0){
                flg=1;
            }
//如果一直为1n那就是间接关系然后就累加几次
            sum +=val.get();
        }
        if(flg==0){
            context.write(key, new IntWritable(sum));
        }
        }
 }

FriendsRecommend

public class FriendsRecommend {
    public static void main(String[] args) throws Exception{
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        job.setJarByClass(FriendsRecommend.class);
        job.setMapperClass(FriendsRecommendMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
   job.setCombinerClass(FriendsRecommendReduce.class);
        job.setReducerClass(FriendsRecommendReduce.class);
        Path inputPath = new Path("/input/friend.txt");
        FileInputFormat.addInputPath(job, inputPath);
        Path outputPath = new Path("/friend/output");
       if (outputPath.getFileSystem(configuration).exists(outputPath))
            outputPath.getFileSystem(configuration).delete(outputPath);
        FileOutputFormat.setOutputPath(job, outputPath);
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

5.打包代码

**6.程序测试 **

创建目录

cd /opt/
ls
cd testData/
mkdir mapreduce
cd mapreduce/

②上传程序

cd /opt/testData/mapreduce
rz -E

③程序测试

分布式文件系统创建input目录并且input目录上传测试文件hello.txt
hdfs dfs -mkdir /input
hdfs dfs -put friend.txt /input
hdfs dfs -ls /input

④查看结果

**7.注意事项 **

在pom.xml中添加依赖时注意路径。将项目编译器中JDK版本与当前项目文件中JDK版本保持一致。

标签: mapreduce 大数据

本文转载自: https://blog.csdn.net/weixin_51598535/article/details/127159048
版权归原作者 19 信息3 吴俊江小组 所有, 如有侵权,请联系我们删除。

“MapReduce课程设计 好友推荐功能”的评论:

还没有评论