Hadoop之MapReduce (统计多个文件夹中单词出现的次数和所在文件夹)

本文介绍如何使用Hadoop MapReduce统计多个文件夹中单词的出现次数,并记录单词出现在哪些文件夹中。通过两步MapReduce作业,首先统计每个文件夹中单词的频次,然后汇总单词在所有文件夹中的分布。

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

统计多个文件夹中单词出现的次数和所在文件夹。

第一步:统计出每个文件夹中单词出现次数。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.log4j.BasicConfigurator;

import java.io.File;
import java.io.IOException;


public class InveseIndexOne {
    public static class InveseIndexOneMap extends Mapper<LongWritable, Text,Text, IntWritable>{
        String FileName = null;
        Text k = new Text();
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            //获取文件名
            FileSplit inputSplit = (FileSplit) context.getInputSplit();
            FileName = inputSplit.getPath().getName();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(" ");
            for (String word:split) {
                k.set(word + "-" + FileName);
                context.write(k,new IntWritable(1));
            }
        }
    }
    public static class InveseIndexOneReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            Integer count = 0;
            for (IntWritable v:values) {
                count++;
            }
            context.write(key,new IntWritable(count));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        BasicConfigurator.configure();//自动快速使用缺省log4j的环境
        Configuration conf = new Configuration();
//        conf.set("yarn.resorcemanager.hostname","192.168.72.110");
//        conf.set("fs.deafutFS", "hdfs://192.168.72.110:9000/");

        Job job = Job.getInstance(conf);

        job.setJarByClass(InveseIndexOne.class);
        job.setMapperClass(InveseIndexOneMap.class);
        job.setReducerClass(InveseIndexOneReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

//        job.setInputFormatClass(TextInputFormat.class);
//        job.setOutputFormatClass(TextOutputFormat.class);


        FileInputFormat.setInputPaths(job,new Path("D:\\eclipse\\wc\\input11"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\eclipse\\wc\\output-count"));

        job.submit();
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

在这里插入图片描述
第二步:以第一步结果为起始,统计单词在每个文件夹中出现了几次。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class InveseIndexTwo {
    public static class InveseIndexTwoMap extends Mapper<LongWritable, Text,Text,Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split("-");
            String word = split[0];
            String index = split[1];
            context.write(new Text(word),new Text(index));
        }
    }

    public static class InveseIndexTwoReduce extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            // 线程不安全的StringBulider效率更高,在此处并不涉及线程安全问题
            StringBuilder builder = new StringBuilder();
            for (Text c:values) {
                builder.append(c).append(",");
            }
            context.write(key,new Text(builder.toString()));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
//        conf.set("yarn.resorcemanager.hostname","192.168.72.110");
//        conf.set("fs.deafutFS", "hdfs://192.168.72.110:9000/");
        Job job = Job.getInstance(conf);

        job.setJarByClass(InveseIndexTwo.class);
        job.setMapperClass(InveseIndexTwoMap.class);
        job.setReducerClass(InveseIndexTwoReduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.setInputPaths(job,new Path("D:\\eclipse\\wc\\output-count"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\eclipse\\wc\\output-"));

        job.submit();
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值