栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

提示:需要安装和配置的地方,在最后的参考材料里面
一、将待分析的文件(不少于100000英文单词)上传到HDFS

 demo.txt为待分析的文件

启动Hadoop

 将该文件上传到hdfs的input文件夹中

 确保上传成功

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

打开Eclipse,配置好MapReduce后能看到该文件

创建MapReduce项目

选中Map/Reduce Project,点击next

填写Project name为WordCount,点击Finish就创建好了项目

为项目添加需要用到的JAR包

点击界面右侧的“Add External JARs…”按钮,弹出如下图所示界面

(1)“/usr/local/hadoop/share/hadoop/common”目录下的 hadoop-common-3.1.3.jar和 haoop-nfs-3.1.3.jar;

(2)“/usr/local/hadoop/share/hadoop/common/lib”目录下的所有 JAR

(3)“/usr/local/hadoop/share/hadoop/mapreduce”目录下的所有 JAR 包,但是,不包括 jdiff、lib、lib-examples 和 sources 目录。

(4)“/usr/local/hadoop/share/hadoop/mapreduce/lib”目录下的所有 JAR包

接着右键点击刚创建的 WordCount 项目,选择 New -> Class

在 Package 处填写 org.apache.hadoop.examples;在 Name 处填写 WordCount

创建 Class 完成后,在 Project 的 src 中就能看到 WordCount.java 这个文件。将如下 WordCount 的代码复制到该文件中

package org.apache.hadoop.examples;
 
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
 
public class WordCount {
    public WordCount() {
    }
 
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
        if(otherArgs.length < 2) {
            System.err.println("Usage: wordcount  [...] ");
            System.exit(2);
        }
 
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.TokenizerMapper.class);
        job.setCombinerClass(WordCount.IntSumReducer.class);
        job.setReducerClass(WordCount.IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
 
        for(int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
 
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
 
    public static class IntSumReducer extends Reducer {
        private IntWritable result = new IntWritable();
 
        public IntSumReducer() {
        }
 
        public void reduce(Text key, Iterable values, Reducer.Context context) throws IOException, InterruptedException {
            int sum = 0;
 
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
 
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
 
    public static class TokenizerMapper extends Mapper {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
 
        public TokenizerMapper() {
        }
 
        public void map(Object key, Text value, Mapper.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
 
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
 
        }
    }
}

在运行 MapReduce 程序前,将/usr/local/hadoop/etc/hadoop中将有修改过的配置文件(如伪分布式需要core-site.xml和hdfs-site.xml),以及 log4j.properties复制到WordCount项目下的src文件夹(~/workspace/WordCount/src)中:

cp /usr/local/hadoop/etc/hadoop/core-site.xml ~/workspace/WordCount/src
cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml ~/workspace/WordCount/src
cp /usr/local/hadoop/etc/hadoop/log4j.properties ~/workspace/WordCount/src

复制完成后,右键点击WordCount选择refresh进行刷新,可以看到文件结构如下所示:

在代码中设置好输入参数。可将代码 main() 函数的 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

改为:

String[] otherArgs=new String[]{"input","output"};

设定参数后,运行程序,可以看到运行成功的提示,刷新DFS Location后也能看到输出的output文件夹

 执行MapReduce项目WordCount后,在 output 的 part-r-00000 这个文件中能看到输出结果

 用命令查看输出结果

 三、将统计结果下载到本地

四、参考材料

安装Ubuntu

http://dblab.xmu.edu.cn/blog/2760-2/

创建Hadoop账户、配置SSH;安装Hadoop、java;Hadoop伪分布式配置

http://dblab.xmu.edu.cn/blog/2441-2/

安装Eclipse、配置 Hadoop-Eclipse-Plugin

http://dblab.xmu.edu.cn/blog/hadoop-build-project-using-eclipse/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686767.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号