【Hadoop】MP程序之统计单词数量

本文对比了两个Java程序,一个是使用Apache Hadoop MapReduce进行单词计数的WordCount.java,另一个是传统的本地文件处理WordCount_Old.java。通过讲解MapReduce的工作原理,展示了如何利用Hadoop处理大规模文本数据,以及MP程序在效率和可扩展性上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

通过MP程序统计单词数量

1. WordCount.java

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.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.log4j.BasicConfigurator;

import java.io.IOException;

/**
 * @author 易上清净
 */
public class WordCount {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 自动快速地使用缺省Log4j环境
        BasicConfigurator.configure();
        Configuration configuration = new Configuration();
        String[] otherArgs = new GenericOptionsParser(configuration, args).getRemainingArgs();
        if (otherArgs.length < 2) {
            System.err.println("必须输入读取文件路径和输出路径");
            System.exit(2);
        }
        Job job = Job.getInstance();
        job.setJarByClass(WordCount.class);
        job.setJobName("Word Count");
        JobConf jobConfiguration = (JobConf) job.getConfiguration();
        // 设置读取文件的路径,都是从HDFS中读取。读取文件路径从脚本文件中传进来
        FileInputFormat.addInputPath(jobConfiguration, new Path(args[0]));
        // 设置mapreduce程序的输出路径,MapReduce的结果都是输入到文件中
        FileOutputFormat.setOutputPath(jobConfiguration, new Path(args[1]));
        // 设置实现了map函数的类
        job.setMapperClass(WordCountMap.class);
        // 设置实现了reduce函数的类
        job.setReducerClass(WordCountReduce.class);
        // 设置reduce函数的key值
        job.setOutputKeyClass(Text.class);
        // 设置reduce函数的value值
        job.setOutputValueClass(IntWritable.class);
        System.exit(job.waitForCompletion(true) ? 0 :1);
    }
}

 2.WordCount_old.java

import java.io.*;
import java.util.HashMap;
public class WordCount_Old  {
    public static void main(String[]args) throws IOException {
        File file = new File("input\\test.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));

        String st;
        HashMap<String, Integer> wordAndCount = new HashMap<>();
        while ((st = br.readLine()) != null) {
            String[] words = st.split(" ");
            for(String word : words) {
                if(wordAndCount.get(word) != null) {
                    int cnt = wordAndCount.get(word) + 1;
                    wordAndCount.put(word, cnt);
                } else {  //该单词第一次出现
                    wordAndCount.put(word, 1);
                }
            }
        }
        System.out.println(wordAndCount.toString());
    }
}

 3.WordCountMap.java

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author 易上清净
 */
public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split(" ");
        for (String s: split) {
            context.write(new Text(s), new IntWritable(1));
        }
    }
}

 4.WordCountReduce.java

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author 易上清净
 */
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        for (IntWritable val: values) {
            count++;
        }
        context.write(key, new IntWritable(count));
    }
}

5.最后再运行一遍WordCount_old.java就是我们所要解答的结果了-统计单词数量-MP程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值