hadoop 第一个程序 wordcount 详解

本文介绍了使用Hadoop实现WordCount的过程,包括配置文件、Mapper和Reducer类的编写,以及如何运行作业。

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

这里写图片描述

package net.csdn.blog.zephyr.main; // 包的命名

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 static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();  ////实例化Configuration
    String[] otherArgs =
        new GenericOptionsParser(conf, args).getRemainingArgs();////GenericOptionsParser是hadoop框架中解析命令行参数的基本类;getRemainingArgs(),返回数组【一组路径】

    if (otherArgs.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2); ////如果只有一个路径,则输出需要有输入路径和输出路径
    } 


    Job job = Job.getInstance(conf);////实例化job
    job.setJarByClass(WordCount.class);////为了能够找到wordcount这个类
    job.setMapperClass(TokenizerMapper.class);////指定map类型
    job.setCombinerClass(IntSumReducer.class);//指定CombinerClass类
    job.setReducerClass(IntSumReducer.class);////指定reduce类
    job.setOutputKeyClass(Text.class);////rduce输出Key的类型,是Text
    job.setOutputValueClass(IntWritable.class);//// rduce输出Value的类型
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));////添加输入路径
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));////添加输出路径
    System.exit(job.waitForCompletion(true) ? 0 : 1);////提交job
  }

  public static class TokenizerMapper extends////继承泛型类Mapper
      Mapper<Object, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);////定义hadoop数据类型IntWritable实例one,并且赋值为1
    private Text word = new Text();////定义hadoop数据类型Text实例word

    public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {////实现map函数
      StringTokenizer itr = new StringTokenizer(value.toString());////Java的字符串分解类,默认分隔符“空格”、“制表符(‘\t’)”、“换行符(‘\n’)”、“回车符(‘\r’)”
      while (itr.hasMoreTokens()) {////循环条件表示返回是否还有分隔符
        word.set(itr.nextToken());////nextToken():返回从当前位置到下一个分隔符的字符串;hadoop全局类context输出函数write;
        context.write(word, one);//word.set()Java数据类型与hadoop数据类型转换
      }
    }
  }

  public static class IntSumReducer extends////继承泛型类Reducer
      Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable result = new IntWritable();
////实例化IntWritable
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
        throws IOException, InterruptedException {////实现reduce
      int sum = 0;
      for (IntWritable val : values) {////循环values,并记录单词个数
        sum += val.get();////Java数据类型sum,转换为hadoop数据类型result
      }
      result.set(sum);
      context.write(key, result);////输出结果到hdfs
    }
 }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值