fork download
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.mapreduce.Mapper;
  6. import org.apache.hadoop.mapreduce.Reducer;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  10.  
  11. import java.io.IOException;
  12.  
  13. public class WordCount {
  14.  
  15. public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
  16. private final static IntWritable one = new IntWritable(1);
  17. private Text word = new Text();
  18.  
  19. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  20. String[] words = value.toString().split("\\s+");
  21. for (String w : words) {
  22. word.set(w);
  23. context.write(word, one);
  24. }
  25. }
  26. }
  27.  
  28. public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  29. private IntWritable result = new IntWritable();
  30.  
  31. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  32. int sum = 0;
  33. for (IntWritable val : values) {
  34. sum += val.get();
  35. }
  36. result.set(sum);
  37. context.write(key, result);
  38. }
  39. }
  40.  
  41. public static void main(String[] args) throws Exception {
  42. Configuration conf = new Configuration();
  43. Job job = Job.getInstance(conf, "word count");
  44. job.setJarByClass(WordCount.class);
  45. job.setMapperClass(TokenizerMapper.class);
  46. job.setCombinerClass(IntSumReducer.class);
  47. job.setReducerClass(IntSumReducer.class);
  48. job.setOutputKeyClass(Text.class);
  49. job.setOutputValueClass(IntWritable.class);
  50.  
  51. FileInputFormat.addInputPath(job, new Path(args[0]));
  52. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  53.  
  54. System.exit(job.waitForCompletion(true) ? 0 : 1);
  55. }
  56. }
  57. public static class CharacterMapper extends Mapper<Object, Text, Text, IntWritable> {
  58. private final static IntWritable one = new IntWritable(1);
  59. private Text character = new Text();
  60.  
  61. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  62. for (int i = 0; i < value.toString().length(); i++) {
  63. character.set(String.valueOf(value.charAt(i)));
  64. context.write(character, one);
  65. }
  66. }
  67. }
Success #stdin #stdout #stderr 0.01s 5284KB
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 5: 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 11: near "import": syntax error
Error: near line 13: near "public": syntax error
Error: near line 17: near "private": syntax error
Error: near line 19: near "public": syntax error
Error: near line 21: near "for": syntax error
Error: near line 23: near "context": syntax error
Error: near line 24: unrecognized token: "}"
Error: near line 31: near "public": syntax error
Error: near line 33: near "for": syntax error
Error: near line 35: unrecognized token: "}"
Error: near line 37: near "context": syntax error
Error: near line 38: unrecognized token: "}"
Error: near line 43: near "Job": syntax error
Error: near line 44: near "job": syntax error
Error: near line 45: near "job": syntax error
Error: near line 46: near "job": syntax error
Error: near line 47: near "job": syntax error
Error: near line 48: near "job": syntax error
Error: near line 49: near "job": syntax error
Error: near line 51: near "FileInputFormat": syntax error
Error: near line 52: near "FileOutputFormat": syntax error
Error: near line 54: near "System": syntax error
Error: near line 55: unrecognized token: "}"
Error: near line 59: near "private": syntax error
Error: near line 61: near "public": syntax error
Error: near line 64: near "context": syntax error
Error: near line 65: unrecognized token: "}"