MapReduce实现词频统计

1.词频统计任务要求
准备两个txt文件分别为wordfile1.txt和wordfile2.txt,内容如下:
在这里插入图片描述
2.在Eclipse中创建项目
我的eclipse在usr/local/eclipse目录下,使用如下命令启动eclipse

cd /usr/local/eclipse
./eclipse

创建一个java工程命名为WordCount,点击next加载jar包
在这里插入图片描述
选中Libraries点击Add External JARS加载jar包
在这里插入图片描述
为了编写一个MapReduce程序,一般需要向Java工程中添加以下JAR包:
(1)“/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-3.0.3.jar和haoop-nfs-3.0.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包。
在这里插入图片描述
3. 编写Java应用程序

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 <in> [<in>...] <out>");
            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 TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public TokenizerMapper() {
        }
        public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString()); 
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
        }
    }
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
        public IntSumReducer() {
        }
        public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.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);
        }
    }
}

在这里插入图片描述
4. 编译打包程序
可以直接点击Eclipse工作界面上部的运行程序的快捷按钮,当把鼠标移动到该按钮上时,在弹出的菜单中选择“Run as”,继续在弹出来的菜单中选择“Java Application”,然后可以把Java应用程序打包生成JAR包,部署到Hadoop平台上运行。现在可以把词频统计程序放在“/usr/local/hadoop/WordCount”目录下。可以用如下命令创建WordCount目录

cd /usr/local/hadoop
mkdir WordCount

在工程名称“WordCount”上点击鼠标右键,在弹出的菜单中选择“Export”,然后选择Runnable JAR file,并将其jar包保存在WordCount目录下,中间过程出现提示选择ok即可。
在这里插入图片描述
然后可以使用如下命令查看jar包是否保存在WordCount目录下

cd /usr/local/hadoop/WordCount
ls

在这里插入图片描述
5.运行程序
运行程序之前需要先使用如下命令启动hadoop平台

cd /usr/local/hadoop
./sbin/start-dfs.sh

登录hadoop平台之后可以用如下命令在hdfs创建两个目录input和output

./bin/hdfs dfs -mkdir /input
./bin/hdfs dfs -mkdir /output
./bin/hdfs dfs -ls /

在这里插入图片描述
在这里插入图片描述
再使用如下命令将之前创建的wordfile1.txt和wordfile2.txt两个文件上传道hadoop平台的input目录下

./bin/hdfs dfs -put /home/hadoop/wordfile1.txt /input
./bin/hdfs dfs -put /home/hadoop/wordfile2.txt /input
./bin/hdfs dfs -ls /input

(这里如果input前面不加“/”表示的是在hdfs中的/user/hadoop/input目录,加上“/”直接就是hdfs中的/input目录
在这里插入图片描述
然后使用如下命令开始运行程序

cd /usr/local/hadoop
./bin/hadoop jar ./WordCount/WordCount.jar /input output

(./WordCount/WordCount.jar表示在/usr/local/hadoop/WordCount目录下的WordCount.jar文件,如果WordCount.jar文件在家目录应改为如下命令)

cd /usr/local/hadoop
./bin/hadoop jar /home/hadoop/WordCount/WordCount.jar /input /output

运行成功后出现如下页面
在这里插入图片描述
运行结束后可以查看output目录下多了两个文件
在这里插入图片描述
词频统计结果已经保存在了hdfs中的/output目录下,使用如下命令查看运行结果

cd /usr/local/hadoop
./bin/hdfs dfs -cat /output
./bin/hdfs dfs -cat /output/part-r-00000

如果运行失败使用如下命令之后再运行上述代码

./bin/hdfs dfs -rm -r /output

运行结果如下图所示
在这里插入图片描述

  • 10
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MapReduce是一种用于处理大规模数据集的编程模型,它可以将大数据集拆分成许多小块进行分布式处理,然后将结果进行合并。实现MapReduce词频统计可以通过以下步骤来完成: 1. 首先,将数据集划分为多个小块,每个小块被称为一个分片。然后,将每个分片传给多个Mapper进行并行处理。Mapper的任务是将输入的分片按行读取,并将每个行中的单词进行拆分,然后为每个单词生成一个键值对,其中键是单词本身,值设为1。 2. Mapper完成后,将生成的键值对传递给Reducer。Reducer的任务是对相同键的多个值进行合并和计数,然后输出结果。Reducer会接收到多个Mapper产生的键值对,首先对键值对按键进行排序,然后对相同键的多个值进行合并,并计算出该键出现的总次数。 3. 最后,将每个键和对应的计数结果作为输出,得到最终的词频统计结果。 为了实现这个过程,可以使用编程语言如Java来编写Map和Reduce函数。在Map函数中,可以使用字符串的分割函数将行拆分为单词,并为每个单词生成一个键值对。在Reduce函数中,可以使用哈希表来合并相同键的值,并计算出总的出现次数。最后,将结果写入输出文件中。 为了实现分布式计算,可以使用分布式计算框架如Hadoop来管理MapReduce任务的调度和运行。Hadoop可以将输入数据切分为多个分片,并将它们分配给集群中的不同节点进行并行处理。同时,Hadoop还能够自动处理节点故障和数据丢失的情况,保证计算的可靠性和高可用性。 通过以上步骤和工具的组合,就可以实现MapReduce词频统计。这种方法可以有效地处理大规模数据集,并获得准确的词频统计结果。同时,由于分布式计算的优势,可以并行处理多个分片,大大提高计算效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值