hadoop共同好友代码
时间: 2023-07-08 18:50:52 浏览: 129
hadoop代码
5星 · 资源好评率100%
以下是Hadoop共同好友的代码示例:
1. 第一个MapReduce作业将原始数据转换为用户和好友之间的关系对:
```java
public class FriendsMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text user = new Text();
private Text friend = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] tokens = value.toString().split("\t");
String userid = tokens[0];
user.set(userid);
if (tokens.length == 2) {
String[] friends = tokens[1].split(",");
for (String friendid : friends) {
friend.set(friendid);
context.write(user, friend);
}
}
}
}
public class FriendsReducer extends Reducer<Text, Text, Text, Text> {
private Text friends = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
for (Text friend : values) {
sb.append(friend.toString()).append(",");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
friends.set(sb.toString());
context.write(key, friends);
}
}
```
2. 第二个MapReduce作业计算每对用户之间的共同好友:
```java
public class CommonFriendsMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text pair = new Text();
private Text friends = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] tokens = value.toString().split("\t");
String[] friendlist = tokens[1].split(",");
for (int i = 0; i < friendlist.length; i++) {
for (int j = i + 1; j < friendlist.length; j++) {
String friend1 = friendlist[i];
String friend2 = friendlist[j];
if (friend1.compareTo(friend2) < 0) {
pair.set(friend1 + "," + friend2);
} else {
pair.set(friend2 + "," + friend1);
}
friends.set(tokens[1]);
context.write(pair, friends);
}
}
}
}
public class CommonFriendsReducer extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
for (Text value : values) {
sb.append(value.toString()).append(",");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
result.set(sb.toString());
context.write(key, result);
}
}
```
上述代码中,第一个作业的Mapper将原始数据中每个用户的好友列表转换为以用户为键、好友为值的键值对,Reducer将同一个用户的好友列表合并为一个字符串。第二个作业的Mapper将第一个作业的输出中的每个用户的好友列表转换为所有好友对的键值对,Reducer将同一个好友对的好友列表合并为一个字符串。
阅读全文