当前位置:   article > 正文

Hadoop最基本的wordcount(统计词频)

Hadoop最基本的wordcount(统计词频)
  1. package com.uniclick.dapa.dstest;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FileSystem;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.LongWritable;
  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. public class WordCount {
  16. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  17. String inputFilePath = "/user/zhouyuanlong/wordcount/input/wordTest*.txt";
  18. String outputFilePath = "/user/zhouyuanlong/wordcount/output/";
  19. String queue = "default";
  20. String jobName = "wordCount";
  21. if(args == null || args.length < 2){
  22. System.out.println("[-INPUT <inputFilePath>"
  23. + "[-OUTPUT <outputFilePath>");
  24. }else{
  25. for(int i=0;i<args.length;i++){
  26. if("-Q".equals(args[i])){
  27. queue = args[++i];
  28. }
  29. }
  30. }
  31. Configuration conf = new Configuration();
  32. conf.set("mapred.job.queue.name", queue);
  33. Job job = new Job(conf, jobName);
  34. job.setJarByClass(WordCount.class);
  35. job.setMapperClass(WordCountMapper.class);
  36. // job.setCombinerClass(cls);
  37. job.setReducerClass(WordCountReducer.class);
  38. job.setOutputKeyClass(Text.class);
  39. job.setOutputValueClass(IntWritable.class);
  40. FileInputFormat.addInputPath(job, new Path(inputFilePath));
  41. Path path = new Path(outputFilePath);
  42. FileSystem fs = FileSystem.get(URI.create(outputFilePath), conf);
  43. if(fs.exists(path)){
  44. // fs.delete(path);
  45. fs.delete(path, true);
  46. }
  47. FileOutputFormat.setOutputPath(job, new Path(outputFilePath));
  48. System.exit(job.waitForCompletion(true) ? 1 : 0);
  49. }
  50. public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
  51. private Text kt = new Text();
  52. private final static IntWritable vt = new IntWritable(1);
  53. public void map(LongWritable key, Text value, Context context)
  54. throws IOException, InterruptedException {
  55. String[] arr = value.toString().split("\t");
  56. for(int i = 0; i < arr.length; i++){
  57. kt.set(arr[i]);
  58. context.write(kt, vt);
  59. }
  60. }
  61. }
  62. public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
  63. private IntWritable vt = new IntWritable();
  64. public void reduce(Text key, Iterable<IntWritable> values, Context context)
  65. throws IOException, InterruptedException{
  66. int sum = 0;
  67. for(IntWritable intVal : values){
  68. sum += intVal.get();
  69. }
  70. vt.set(sum);
  71. context.write(key, vt);
  72. }
  73. }
  74. }


input目录中文件wordTest1.txt的内容(每行以table键分隔):

hello    world
hello    hadoop
hello    mapredruce


input目录中文件wordTest2.txt的内容(每行以table键分隔):

hello    world
hello    hadoop
hello    mapredruce

hdfs输出结果:

web     2
mapredruce      1
python  1
hadoop  1
hello   6
clojure 2
world   1
java    2


PS:对Hadoop自带的wordcount的例子略有改变

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/731438
推荐阅读
相关标签
  

闽ICP备14008679号