hadoop之wordcount

布满荆棘的人生 2022-06-10 05:11 331阅读 0赞

1、搭建好一个hadoop程序:

hadoop完全伪分布式搭建

2、在myeclipse的安装目录下导入hadoop插件:
这里写图片描述

效果:
这里写图片描述
这里我们解压一个hadoop的安装包,箭头指向的位置引入所需依赖包

3、创建一个工程
这里写图片描述

4、window–>show view –>Map/Reduce Location
这里写图片描述

5、编写WordCount程序:

  1. package com.hadoop.hdfs;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.LongWritable;
  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. /** * * @author Howie Lee * 继承Mapper<...>四个泛型分别对应输入的key value 和输出的key value * * BooleanWritable:标准布尔型数值 * ByteWritable:单字节数值 * DoubleWritable:双字节数 * FloatWritable:浮点数 * IntWritable:整型数 * LongWritable:长整型数 * Text:使用UTF8格式存储的文本 * NullWritable:当<key,value>中的key或value为空时使用 * * */
  14. public static class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
  15. protected void map(LongWritable key, Text value, Context context)
  16. throws java.io.IOException ,InterruptedException {
  17. //分割每行数据
  18. String line = value.toString();
  19. String[] words = line.split(" ");
  20. for(String word : words){
  21. context.write(new Text(word), new LongWritable(1));
  22. }
  23. };
  24. }
  25. /** * * @author Howie Lee * 继承Reducer<...>四个泛型分别对应输入的key value 和输出的key value */
  26. public static class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
  27. protected void reduce(Text key, Iterable<LongWritable> values, Context context)
  28. throws java.io.IOException ,InterruptedException {
  29. Long sum = 0L;
  30. for(LongWritable value : values ){
  31. //这里使用value.get();将LongWritable转换为int类型
  32. sum += value.get();
  33. }
  34. context.write(key, new LongWritable(sum));
  35. };
  36. }
  37. public static void main(String[] args) throws IOException, Exception {
  38. Configuration conf = new Configuration();//指定作业执行规范
  39. Job job = Job.getInstance(conf);//初始化一个job对象
  40. //传入的class 找到job的jar包(这里只是使用这个类去寻找他的包)
  41. job.setJarByClass(WordCount.class);
  42. job.setMapperClass(WCMapper.class);//指定map函数
  43. job.setReducerClass(WCReducer.class);//指定reducer函数
  44. job.setOutputKeyClass(Text.class);//输出key格式(如果一样的话,这句话就可以做到输出map和reducer的格式)
  45. //如果map/reducer输出不同就单独指定map的
  46. job.setMapOutputKeyClass(Text.class);//输出map的key格式
  47. job.setOutputValueClass(LongWritable.class);//输出value格式
  48. job.setMapOutputValueClass(LongWritable.class);//输出map的value格式
  49. FileInputFormat.addInputPath(job, new Path("/word/"));//处理文件路径
  50. FileOutputFormat.setOutputPath(job, new Path("/output/"));//结果输出路径
  51. job.waitForCompletion(true);
  52. }
  53. }

6、导出为一个jar包(File–>Export–> Jar file)
这里写图片描述

7、运行
这里写图片描述

8、查看运行结果
这里写图片描述

完成

发表评论

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

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

相关阅读