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

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

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

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);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题可以通过编写一个 MapReduce 程序来解决。以下是一个简单的 MapReduce 程序来对 Hadoop.txt 各个单词出现次数进行统计的示例: 1. Map 阶段 在 Map 阶段,我们将输入文件 Hadoop.txt 的每一行分解成单词,并将每个单词映射到一个键值对,其键是单词本身,值为 1,表示该单词出现了一次。 ```java public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable ONE = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, ONE); } } } ``` 2. Reduce 阶段 在 Reduce 阶段,我们将相同单词的键值对合并,并将它们的值相加,以得到每个单词出现的总次数。 ```java public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } ``` 3. 驱动程序 在驱动程序,我们指定输入输出路径,以及 Mapper 和 Reducer 类。 ```java public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上代码可以将 Hadoop.txt 各个单词出现次数进行统计,并将结果输出到指定的输出路径

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值