当前位置:   article > 正文

调用MapReduce对文件中各个单词出现的次数进行统计_mapreduce统计单词出现次数

mapreduce统计单词出现次数

需求描述

要求:

1.将待分析的文件(不少于10000英文单词)上传到HDFS

2.调用MapReduce对文件中各个单词出现的次数进行统计。

3.将统计结果下载本地。

注意:需要实现的是调用MapReduce对文件中各个单词出现的次数进行统计。要求在Linux系统中实现上述操作。首先要安装Ubuntu系统,然后要配置Java环境,安装JDK。Ubuntu提供了一个健壮,功能丰富的计算环境。

环境介绍

Ubuntu 14.04

Hadoop 2.6.0(伪分布式)

Eclipse 3.8

实验步骤

一、准备环境

1. 安装Eclipse

 

安装完成后打开eclipse

2. 在Eclipse中创建项目并添加需要用到的JAR包

第一次打开需要填写workspace(工作空间),保持默认即可

选择“File->New->Java Project”菜单,开始创建一个Java工程

添加需要的jar包,为了编写一个能够与HDFS交互的Java应用程序,一般需要向Java工程中添加以下JAR包:
(1)”/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar;
(2)/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包;
(3)“/usr/local/hadoop/share/hadoop/hdfs”目录下的haoop-hdfs-2.7.1.jar和haoop-hdfs-nfs-2.7.1.jar;
(4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目录下的所有JAR包。
比如,如果要把“/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar添加到当前的Java工程中,可以在界面中点击目录按钮,进入到common目录,然后,界面会显示出common目录下的所有内容


3. 编写并编译运行Java应用程序

鼠标右键点击你创建的工程,选择“New->Class”菜单,

我这里是之前做的一个实验判断HDFS中是否存在某文件,程序名为HDFSFileIfExist.java,程序代码如下

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.FileSystem;
  3. import org.apache.hadoop.fs.Path;
  4. public class HDFSFileIfExist {
  5. public static void main(String[] args){
  6. try{
  7. String fileName = "test";
  8. Configuration conf = new Configuration();
  9. conf.set("fs.defaultFS", "hdfs://localhost:9000");
  10. conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
  11. FileSystem fs = FileSystem.get(conf);
  12. if(fs.exists(new Path(fileName))){
  13. System.out.println("文件存在");
  14. }else{
  15. System.out.println("文件不存在");
  16. }
  17. }catch (Exception e){
  18. e.printStackTrace();
  19. }
  20. }
  21. }


4.编译运行程序

在开始编译运行程序之前,请一定确保Hadoop已经启动运行,如果还没有启动,需要打开一个Linux终端,输入以下命令启动Hadoop:

  1. cd /usr/local/hadoop
  2. ./sbin/start-dfs.sh
  3. jps#执行出现namenode secondnode datanode证明启动成功

 现在就可以编译运行上面编写的代码。可以直接点击Eclipse工作界面上部的运行程序的快捷按钮,当把鼠标移动到该按钮上时,在弹出的菜单中选择“Run As”,继续在弹出来的菜单中选择“Java Application”

 到这里说明eclipse运行所需要的环境都没问题,接下来就是执行MapReduce程序完成这次实验的要求:词频统计。

 

二、调用MapReduce对文件中各个单词出现的次数进行统计

1.配置 eclipse运行MapReduce需要的插件及环境请参考下面的文章:

使用Eclipse编译运行MapReduce程序_Hadoop2.6.0_Ubuntu/CentOS_厦大数据库实验室博客 (xmu.edu.cn)

2.配置好需要的插件后创建MapReduce程序

点击File选择new project 按照下图操作:

创建好后鼠标右键点击新建class 名称取WordCount即可。

3.程序代码(在WordCount.java输入下面代码)

  1. package org.apache.hadoop.examples;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.StringTokenizer;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.util.GenericOptionsParser;
  15. public class WordCount {
  16. public WordCount() {
  17. }
  18. public static void main(String[] args) throws Exception {
  19. Configuration conf = new Configuration();
  20. String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
  21. if(otherArgs.length < 2) {
  22. System.err.println("Usage: wordcount <in> [<in>...] <out>");
  23. System.exit(2);
  24. }
  25. Job job = Job.getInstance(conf, "word count");
  26. job.setJarByClass(WordCount.class);
  27. job.setMapperClass(WordCount.TokenizerMapper.class);
  28. job.setCombinerClass(WordCount.IntSumReducer.class);
  29. job.setReducerClass(WordCount.IntSumReducer.class);
  30. job.setOutputKeyClass(Text.class);
  31. job.setOutputValueClass(IntWritable.class);
  32. for(int i = 0; i < otherArgs.length - 1; ++i) {
  33. FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
  34. }
  35. FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
  36. System.exit(job.waitForCompletion(true)?0:1);
  37. }
  38. public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  39. private IntWritable result = new IntWritable();
  40. public IntSumReducer() {
  41. }
  42. public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  43. int sum = 0;
  44. IntWritable val;
  45. for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
  46. val = (IntWritable)i$.next();
  47. }
  48. this.result.set(sum);
  49. context.write(key, this.result);
  50. }
  51. }
  52. public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
  53. private static final IntWritable one = new IntWritable(1);
  54. private Text word = new Text();
  55. public TokenizerMapper() {
  56. }
  57. public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  58. StringTokenizer itr = new StringTokenizer(value.toString());
  59. while(itr.hasMoreTokens()) {
  60. this.word.set(itr.nextToken());
  61. context.write(this.word, one);
  62. }
  63. }
  64. }
  65. }

4.运行:右键选择Run as ->run on Hadoop

显示已存在output文件就删除,不存在input文件就创建

  1. cd /usr/local/hadoop
  2. ./bin/hdfs dfs -rm -r output #删除output文件夹
  3. ./bin/hdfs dfs -mkdir input #创建input文件 如果出错就加上 -p

 5.为了方便命令行执行java打包的程序,先创建一个test文件夹保存打包的程序

mkdir /usr/local/hadoop/test

 

在工程名称“WordCount”上点击鼠标右键,在弹出的菜单中选择“Export”,弹出下图界面之后点击Next,再接着点Next
在这里插入图片描述
在这里插入图片描述
4.点击"Finsh“之后弹出以下界面之后可以点击ok,忽略它

在这里插入图片描述
在这里插入图片描述

 在运行程序之前,需要启动Hadoop,启动Hadoop之后,需要首先删除HDFS中与当前Linux用户hadoop对应的input和output目录,命令上面有。

在Linux本地文件系统中新建的文件上传到HDFS的“/user/hadoop/input”目录下(我是在网上找10000字的英语文章),命令如下:

  1. cd /usr/local/hadoop
  2. ./bin/hdfs dfs -put ./wordtets.txt input #wordtest.txt是待词频统计的文本文件
  3. ./bin/hdfs dfs -ls input #查看是否上传成功

最后查看文本已经上传成功了

使用hadoop jar命令运行程序,命令如下
 

  1. cd /usr/local/hadoop
  2. ./bin/hadoop jar ./test/WordCount.jar input output

 

 最后查看统计结果:

./bin/hdfs dfs -cat output/part-r-00000

出现统计结果

 下载统计结果到本地:

  1. ./bin/hdfs dfs -get output
  2. ls #出现的output目录就是下载到本地的统计结果
  3. cat ./output/part-r-00000 #结果和HDFS上的output中的一样

 这样全部要求就完成了

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

闽ICP备14008679号