一个简单的mapreduce程序---统计每个单词出现次数

本文详细介绍使用Hadoop实现单词计数的过程,包括Mapper、Reducer和Driver类的定义,以及如何通过MapReduce框架统计文本中单词出现的频率。

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

因为会对key进行排序,所以要把需要排序的元素写到key中

例如:统计单词出现次数,并按照次数降序。

           可以这样:map阶段输出<单词,1>,reduce阶段输出<单词,n>,map阶段输出<n,单词>,reduce阶段输出<单词,n>

 

统计每个单词出现次数

1.  定义一个Mapper类

2.  定义一个Reducer类

3.  定义一个Driver类(本机模拟运行)

附代码:

https://github.com/YanYuPS/wordCountSimple.git
//Map:每个单词记一个1
//输入key(一行文本的偏移量),输入value(一行文本内容),输出key(单词),输出value(单词个数)
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

   //MapTask调用map方法,每读取一个(keyIn,valueIn),就调用一次map方法
   @Override
   protected void map(LongWritable key, Text value, Context context)
         throws IOException, InterruptedException {
         
      //获取每一行的文本内容
      String lines = value.toString();
      String[] words = lines.split(" ");
      
      for (String word :words) {
         context.write(new Text(word), new IntWritable(1));
      } 
   }
}
//输入key(map的输出key:单词),输入value(map的输出value:单词个数),输出key(单词),输出value(单词个数)
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

   //ReduceTask调用reduce方法:会先将接收到的kv对按照key分组(key相同为一组);然后每组key调用一次reduce方法
   @Override
   protected void reduce(Text key, Iterable<IntWritable> values,
         Context context) throws IOException, InterruptedException {
      
      int count =0;
      for(IntWritable v :values){
         count += v.get();
      }
      context.write(key, new IntWritable(count));
   }
}
//主类,指定job参数并提交job
public class WordCountDriver {
   
   public static void main(String[] args) throws Exception {

      Configuration conf = new Configuration();
      Job job = Job.getInstance(conf);

      //jar包位置
      job.setJarByClass(WordCountDriver.class);
      
      //指定mapper类和reducer类
      job.setMapperClass(WordCountMapper.class);
      job.setReducerClass(WordCountReducer.class);
      
      //map阶段,输出的数据类型
      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("/Users/rainbow/Desktop/wordin"));
      
      //处理结果存放文件夹路径(如已存在,需删除)
      FileOutputFormat.setOutputPath(job, new Path("/Users/rainbow/Desktop/wordout"));

      //向yarn集群提交job
      boolean res = job.waitForCompletion(true);
      System.exit(res?0:1);
   }
}

 

 

 

好的,这个问题可以通过编写一个 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 中各个单词出现的次数进行统计,并将结果输出到指定的输出路径中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值