fork download
  1. import java.io.IOException;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Reducer;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15.  
  16. public class FriendsOfFriends {
  17.  
  18. public static class FriendsMapper extends Mapper<Object, Text, Text, Text> {
  19.  
  20. private Text userPair = new Text();
  21. private Text mutualFriend = new Text();
  22.  
  23. @Override
  24. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  25. String[] tokens = value.toString().split("\t");
  26. if (tokens.length == 2) {
  27. String user = tokens[0];
  28. String[] friends = tokens[1].split(",");
  29. Arrays.sort(friends); // Sort the friends to ensure consistent key ordering
  30. for (int i = 0; i < friends.length; i++) {
  31. for (int j = i + 1; j < friends.length; j++) {
  32. userPair.set(friends[i] + "," + friends[j]);
  33. mutualFriend.set(user);
  34. context.write(userPair, mutualFriend);
  35. }
  36. }
  37. }
  38. }
  39. }
  40.  
  41. public static class FriendsReducer extends Reducer<Text, Text, Text, IntWritable> {
  42.  
  43. private IntWritable mutualFriendsCount = new IntWritable();
  44.  
  45. @Override
  46. public void reduce(Text key, Iterable<Text> values, Context context)
  47. throws IOException, InterruptedException {
  48. Set<String> mutualFriendsSet = new HashSet<>();
  49. for (Text value : values) {
  50. mutualFriendsSet.add(value.toString());
  51. }
  52. mutualFriendsCount.set(mutualFriendsSet.size());
  53. context.write(key, mutualFriendsCount);
  54. }
  55. }
  56.  
  57. public static void main(String[] args) throws Exception {
  58. Configuration conf = new Configuration();
  59. Job job = Job.getInstance(conf, "FriendsOfFriends");
  60. job.setJarByClass(FriendsOfFriends.class);
  61. job.setMapperClass(FriendsMapper.class);
  62. job.setReducerClass(FriendsReducer.class);
  63. job.setOutputKeyClass(Text.class);
  64. job.setOutputValueClass(Text.class);
  65. FileInputFormat.addInputPath(job, new Path(args[0]));
  66. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  67. System.exit(job.waitForCompletion(true) ? 0 : 1);
  68. }
  69. }
Success #stdin #stdout #stderr 0.01s 5304KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 1: near "import": syntax error
Error: near line 2: near "import": syntax error
Error: near line 3: near "import": syntax error
Error: near line 4: near "import": syntax error
Error: near line 6: near "import": syntax error
Error: near line 7: near "import": syntax error
Error: near line 8: near "import": syntax error
Error: near line 9: near "import": syntax error
Error: near line 10: near "import": syntax error
Error: near line 11: near "import": syntax error
Error: near line 12: near "import": syntax error
Error: near line 13: near "import": syntax error
Error: near line 14: near "import": syntax error
Error: near line 16: near "public": syntax error
Error: near line 21: near "private": syntax error
Error: near line 23: near "@Override": syntax error
Error: near line 26: near "if": syntax error
Error: near line 28: near "String": syntax error
Error: near line 29: near "Arrays": syntax error
Error: near line 33: near "mutualFriend": syntax error
Error: near line 34: near "context": syntax error
Error: near line 35: unrecognized token: "}"
Error: near line 45: near "@Override": syntax error
Error: near line 49: near "for": syntax error
Error: near line 51: unrecognized token: "}"
Error: near line 53: near "context": syntax error
Error: near line 54: unrecognized token: "}"
Error: near line 59: near "Job": syntax error
Error: near line 60: near "job": syntax error
Error: near line 61: near "job": syntax error
Error: near line 62: near "job": syntax error
Error: near line 63: near "job": syntax error
Error: near line 64: near "job": syntax error
Error: near line 65: near "FileInputFormat": syntax error
Error: near line 66: near "FileOutputFormat": syntax error
Error: near line 67: near "System": syntax error
Error: near line 68: unrecognized token: "}"