我正在尝试实现共同的朋友代码。在进入reducer后成功运行此代码的映射器后,它会抛出错误而无法产生所需的输出。
我有以下驱动程序类:
public class MutualFriends {
public static void main(String args[]) throws Exception {
// Standard Job setup procedure.
Configuration conf = new Configuration();
long milliseconds = 1000*60*60;
conf.setLong("mapreduce.task.timeout",milliseconds);
Job job = new Job(conf, "Mutual Friends");
job.setJarByClass(MutualFriends.class);
job.setMapperClass(FriendsMapper.class);
//job.setReducerClass(FriendsReducer.class);
//job.setCombinerClass(FriendsReducer.class);
job.setReducerClass(FriendsReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
映射
public static class FriendsMapper
extends Mapper<Object, Text, Text, Text> {
private Text m_id = new Text();
private Text m_others = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
// In our case, the key is null and the value is one line of our input file.
// Split by space to separate the user and its friends list.
String line = value.toString();
String[] split = line.split(" ");
String subject = split[0];
String[] friends = Arrays.copyOfRange(split, 1, split.length);
// For each friend in the list, output the (UserFriend, ListOfFriends) pair
for(String friend : friends) {
String others = line.replace(subject, "").replace(" ", "");
String id = subject.compareTo(friend) < 0 ? subject+friend : friend+subject;
m_id.set(id);
m_others.set(others);
context.write(m_id, m_others);
}
}
}
减速机:
public static class FriendsReducer
extends Reducer<Text, Text, Text, Text> {
private Text m_result = new Text();
// Calculates intersection of two given Strings, i.e. friends lists
public String intersection(String s1, String s2) {
HashSet<Character> h1 = new HashSet<Character>();
HashSet<Character> h2 = new HashSet<Character>();
for(int i = 0; i < s1.length(); i++) {
h1.add(s1.charAt(i));
}
for(int j = 0; j < s2.length(); j++) {
h2.add(s2.charAt(j));
}
h1.retainAll(h2);
Character[] res = h1.toArray(new Character[0]);
String intersect = new String("");
for (int i = 0; i < res.length; i++) {
intersect += res[i];
}
char[] letters = intersect.toCharArray();
Arrays.sort(letters);
String sortedIntersect = new String(letters);
return sortedIntersect;
}
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
// Prepare a 2-String-Array to hold the values, i.e. the friends lists of
// our current friends pair.
String[] combined = new String[2];
int cur = 0;
for(Text value : values) {
combined[cur++] = value.toString();
}
// Calculate the intersection of these lists and write result in the form (UserAUserB, MutualFriends).
m_result.set(intersection(combined[0], combined[1]));
context.write(key, m_result);
}
}
错误:
15/09/22 16:27:29 INFO mapreduce.Job: Task Id : attempt_1442915698147_0006_r_000000_0, Status : FAILED
Error: java.lang.NullPointerException
at MutualFriends$FriendsReducer.intersection(MutualFriends.java:54)
at MutualFriends$FriendsReducer.reduce(MutualFriends.java:82)
at MutualFriends$FriendsReducer.reduce(MutualFriends.java:42)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:171)
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:163)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1628)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
目前M使用: Ubuntu 14-04 Hadoop 2.6.0