hadoop wordcount

我会带着你远行 2022-06-07 10:14 347阅读 0赞

在hdfs上的文本内容如下

  1. hello world hello java
  2. hello c
  3. hello hadoop map reduce

以下是自己对这个过程的总结

  1. mapreduce执行的流程 input<k1,v1> -> map -><k2,v2> -> <k2,list<v2>> ->reduce<k3,v3> ->output 具体的步骤: map输入 <0,hello world hello java> <22, hello c> <29, hello hadoop map reduce> map输出 <hello,1> <world,1> <hello,1> <java, 1> <hello,1 > <c, 1> <hello, 1> <hadoop , 1> <map, 1> <reduce, 1> 进行shuffle map的输出进行排序分组 shuffle处理后 <hello, <1,1,1,1>> <world, <1>> <java, <1>> <c, <1>>... reduce 接受shuffle后的kv,遍历v的列表,进行求和 结果: <hello, 4> <world,1>... outputhdfs

代码

  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. public class WordCount {
  13. /** * * map: * 将一行句子, 以空格切分 进行输出 <一个单词, 1> * @author hadoop * */
  14. public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
  15. private final static IntWritable one = new IntWritable(1);
  16. private Text word = new Text();
  17. public void map(Object key, Text value, Context context
  18. ) throws IOException, InterruptedException {
  19. //String[] strs = value.toString().split("");
  20. StringTokenizer itr = new StringTokenizer(value.toString());
  21. while (itr.hasMoreTokens()) {
  22. word.set(itr.nextToken());
  23. context.write(word, one);
  24. }
  25. }
  26. }
  27. /** * reduce功能 * 接受map的数据(中间有shuffle过程) * 计数 * @author hadoop * */
  28. public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
  29. private IntWritable result = new IntWritable();
  30. public void reduce(Text key, Iterable<IntWritable> values,
  31. Context context
  32. ) throws IOException, InterruptedException {
  33. int sum = 0;
  34. for (IntWritable val : values) {
  35. sum += val.get();
  36. }
  37. result.set(sum);
  38. context.write(key, result);
  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. //设置主要的工作类
  45. job.setJarByClass(WordCount.class);
  46. //设置输入输出路径
  47. FileInputFormat.addInputPath(job, new Path(args[0]));
  48. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  49. //设置map和reduce类
  50. job.setMapperClass(TokenizerMapper.class);
  51. job.setReducerClass(IntSumReducer.class);
  52. //设置输出k, v 格式
  53. job.setOutputKeyClass(Text.class);
  54. job.setOutputValueClass(IntWritable.class);
  55. //job.setCombinerClass(IntSumReducer.class);
  56. //运行任务
  57. System.exit(job.waitForCompletion(true) ? 0 : 1);
  58. }
  59. }

发表评论

表情:
评论列表 (有 0 条评论,347人围观)

还没有评论,来说两句吧...

相关阅读