文章目录
前言
本文章介绍好友推荐项目:
内容:
– 非好友的两个人之间存在相同好友则互为推荐关系
– 朋友圈两个非好友的人,存在共同好友人数越多,越值得推荐
– 存在一个共同好友,值为1;存在多个值累加
一、程序需求
1.需求
• 程序要求,给每个人推荐可能认识的人
– 互为推荐关系值越高,越值得推荐
– 每个用户,推荐值越高的可能认识的人排在前面
2. 数据
• 数据使用空格分割
• 每行是一个用户以及其对应的好友
• 每行的第一列名字是用户的名字,后面的是其对应的好友
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.结果
需得到如上结果
二、操作步骤
1.创建项目
- 创建一个friends-recommend模块
New-----Module
- Maven–>next
- 新建类和包
创建org.hadoop包
分别创建FriendsRecommend、 FriendsRecommendMapper、 FriendsRecommendReduce三个类
2.引入Jar包
在pom.xml里面加入以下代码
注: 导入Jar包时务必联网
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
- 导入打包插件
在pom.xml里面加入以下代码:
注: 导入Jar包时务必联网
<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>
3.编写代码
FriendsRecommend代码:
package org.hadoop;
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 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("/friend/input");
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);
}
}
FriendsRecommendMapper代码:
package org.hadoop;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class FriendsRecommendMapper extends Mapper<Object, Text, Text, IntWritable> {
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
//对好友关系进行拆分
String[] friends = value.toString().split("\\s");
//开始关联直接好友
for (int i = 1; i < friends.length; i++) {
context.write(new Text(sortFriendName(friends[0], friends[i])), new IntWritable(0));
}
//开始关联间接好友
for (int i = 1; i < friends.length; i++) {
for (int j = i + 1; j < friends.length; j++) {
context.write(new Text(sortFriendName(friends[i], friends[j])), new IntWritable(1));
}
}
}
/**
* 按照字典序排序两个好友的名字组合顺序
*/
private String sortFriendName(String f1, String f2) {
return f1.compareTo(f2) < 0 ? f1 + ":" + f2 : f2 + ":" + f1;
}
}
FriendsRecommendReduce代码:
package org.hadoop;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.Iterator;
public class FriendsRecommendReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
//定义一个计数器
int count = 0;
//获取迭代器
Iterator<IntWritable> iterator = values.iterator();
while (iterator.hasNext()) {
//获取推荐度
int recommendation = iterator.next().get();
//判断
if (recommendation == 0) {
return;
} else {
count += recommendation;
}
}
//将推荐度写出到HDFS
context.write(key, new IntWritable(count));
}
}
4.程序打包
在父项目的Maven视图下双击package进行打包
打包成功后可在相应的本地获得Jar包,如下图所示:
三、程序测试
注:在此之前需确保Hadoop集群打开
1.创建目录
mkdir testData
mkdir friends
cd /opt/testData/friends
2.上传测试数据
1.将之前打包好的Jar包上传到虚拟机node01里面
2.将本地测试文件data.txt上传到friend/input里面
3.执行程序
运行成功后输入:hdfs dfs -cat/friend/output出现以下代码表示成功
4.查看运行结果
四、总结:
以上就是今天的练习项目 ,实现好友推荐,喜欢记得三连哦!!!