一、程序要求

给每个人推荐可能认识的人
互为推荐关系值越高,越值得推荐
每个用户,推荐值越高的可能认识的人排在前面

二、项目说明

互为推荐关系:
存在一个共同好友,值为1,存在多个,值累加
非好友的两个人之间存在相同好友则互为推荐关系
朋友圈两个非好友的人,存在共同好友人数越多,越值得推荐
数字表示他们共有好友数

三、数据

3.1数据准备

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

3.2数据说明

• 数据使用空格分割
• 每行是一个用户以及其对应的好友
• 每行的第一列名字是用户的名字,后面的是其对应的好友
• 将数据保存到friend.txt
• 将数据上传到hdfs中

四、程序实现

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 = Job.getInstance(configuration);
        job.setJarByClass(FriendsRecommend.class);
        //Map端
        job.setMapperClass(FriendsRecommendMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //combiner组件
//      job.setCombinerClass(FriendsRecommendReduce.class);
        //Reduce端
        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);
    }
}

五、代码打包

在pom.xml中添加依赖

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.hadoop.FriendsRecommend</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

org.hadoop.FriendsRecommend中的路径应该和项目路径一致
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

六、程序测试

6.1创建目录

cd /opt/
ls # 如果目录下没有testData目录的话自己手动创建一下即可
cd testData/
mkdir mapreduce
cd mapreduce/
在这里插入图片描述

6.2上传程序 xxx.jar

cd /opt/testData/mapreduce
rz -E
上传程序时如果安装了插件可以使用rz -E,如果没安装插件就使用Xftp上传程序。在这里插入图片描述

6.3程序测试

首先上传本地测试文件friends.txt到虚拟机上(与上传程序jar包步骤相同)
在这里插入图片描述
确认Hadoop集群已经开启,启动命令:start-dfs.sh和start-yarn.sh
在这里插入图片描述
分布式文件系统创建input目录并且input目录上传测试文件hello.txt
hdfs dfs -mkdir /input
hdfs dfs -put friend.txt /input
hdfs dfs -ls /input
在这里插入图片描述
执行程序
hadoop jar friends-1.0-SNAPSHOT.jar /input /output
在这里插入图片描述

6.4查看结果

hdfs dfs -cat /friends/output/part-r-00000
在这里插入图片描述

七、注意事项

7.1可能的错误

  1. 在pom.xml中添加依赖时路径需要不一致一致
  2. 虚拟机上报错误:java.lang.UnsupportedClassVersionError

7.2解决方法

  1. 在pom.xml中添加依赖时注意路径
  2. 这个问题是由于使用较高版本的JDK编译的java class文件试图在较低版本的JVM上运行产生的错误。
    将项目编译器中JDK版本与当前项目文件中JDK版本保持一致。
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐