已知 MapReduce 程序代码块为:
public static class UserMapper extends Mapper<LongWritable, Text, Text, Text> {
public void map(LongWritable key, Text value, Context context) {
try {
String line = value.toString();
String[] words = line.split(",");
String userName = words[0];
String friendName = words[1];
context.write(new Text(userName), new Text(friendName));
context.write(new Text(friendName), new Text(userName));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class UserReducer extends Reducer<Text, Text, Text, Text> {
List<String> userList = new LinkedList<String>();
Map<String, Integer> userFriends = new HashMap<String, Integer>();
public void reduce(Text key, Iterable<Text> values, Context context) {
try {
String userName = key.toString();
if(!userList.contains(userName)){
userList.add(userName);
}
List<String> friendsList = new ArrayList<String>();
for (Text value : values) {
String friendName=value.toString();
if(!friendsList.contains(friendName)){
friendsList.add(friendName);
}
}
userFriends.put(userName,friendsList.size());
} catch (Exception e) {
e.printStackTrace();
}
}
protected void cleanup(Context context) {
try {
int userCout = userList.size();
Iterator<Map.Entry<String, Integer>> itr=userFriends.entrySet().iterator();
while (itr.hasNext()){
Map.Entry<String, Integer> word=itr.next();
String key=word.getKey();
int value=word.getValue();
DecimalFormat df=new DecimalFormat("0.00");
context.write(new Text(key), new Text(df.format((float)value/userCout)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
内容如下的文件经过该程序处理后的输出结果为()
张三,赵八 李四,陈七 王五,陈七 钱六,张三 陈七,钱六 赵八,李四

