Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. /*
  2. **************************************************************
  3. Platform
  4. **************************************************************
  5. jdk: 1.8.x
  6. hadoop: 3.1.0
  7.  
  8. **************************************************************
  9. Quick Guide
  10. **************************************************************
  11. input_dir=/user/input
  12. output_dir=/user/output
  13.  
  14. # prepare folder in HDFS
  15. hadoop fs -mkdir ${input_dir}
  16. hadoop fs -rm -r ${output_dir}
  17. hadoop fs -put ~//sample.txt ${input_dir}
  18.  
  19. # compile java
  20. javac -classpath hadoop-core-1.2.1.jar -d ../bin WordCount.java
  21. jar -cvf WordCount.jar -C ../bin .
  22. hadoop jar WordCount.jar WordCount ${input_dir} ${output_dir}
  23.  
  24. # show the result
  25. hadoop fs -ls $output_dir
  26. hadoop fs -cat $output_dir/part-r-00000
  27. */
  28.  
  29. import java.io.IOException;
  30. import java.util.StringTokenizer;
  31.  
  32. import org.apache.hadoop.conf.Configuration;
  33. import org.apache.hadoop.fs.Path;
  34. import org.apache.hadoop.io.IntWritable;
  35. import org.apache.hadoop.io.Text;
  36. import org.apache.hadoop.mapreduce.Job;
  37. import org.apache.hadoop.mapreduce.Mapper;
  38. import org.apache.hadoop.mapreduce.Reducer;
  39. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  40. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  41.  
  42. public class WordCount {
  43.  
  44. public static class TokenizerMapper
  45. extends Mapper<Object, Text, Text, IntWritable>{
  46.  
  47. private final static IntWritable one = new IntWritable(1);
  48. private Text word = new Text();
  49.  
  50. public void map(Object key, Text value, Context context
  51. ) throws IOException, InterruptedException {
  52. StringTokenizer itr = new StringTokenizer(value.toString());
  53. while (itr.hasMoreTokens()) {
  54. word.set(itr.nextToken());
  55. context.write(word, one);
  56. }
  57. }
  58. }
  59.  
  60. public static class IntSumReducer
  61. extends Reducer<Text,IntWritable,Text,IntWritable> {
  62. private IntWritable result = new IntWritable();
  63.  
  64. public void reduce(Text key, Iterable<IntWritable> values, Context context
  65. ) throws IOException, InterruptedException {
  66. int sum = 0;
  67. for (IntWritable val : values) {
  68. sum += val.get();
  69. }
  70. result.set(sum);
  71. context.write(key, result);
  72. }
  73. }
  74.  
  75. public static void main(String[] args) throws Exception {
  76. Configuration conf = new Configuration();
  77. Job job = Job.getInstance(conf, "word count");
  78. job.setJarByClass(WordCount.class);
  79. job.setMapperClass(TokenizerMapper.class);
  80. job.setCombinerClass(IntSumReducer.class);
  81. job.setReducerClass(IntSumReducer.class);
  82. job.setOutputKeyClass(Text.class);
  83. job.setOutputValueClass(IntWritable.class);
  84. FileInputFormat.addInputPath(job, new Path(args[0]));
  85. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  86. System.exit(job.waitForCompletion(true) ? 0 : 1);
  87. }
  88. }
Add Comment
Please, Sign In to add comment