千家信息网

hadoop中wordcount 、wordmean的示例代码

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,小编给大家分享一下hadoop中wordcount 、wordmean的示例代码,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!注意hadoop src 可以在 hadoop官网上下载
千家信息网最后更新 2026年11月04日hadoop中wordcount 、wordmean的示例代码

小编给大家分享一下hadoop中wordcount 、wordmean的示例代码,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

注意

  1. hadoop src 可以在 hadoop官网上下载。

  2. 分析中/**开头的为源码自带,//开头的为作者心得。

  3. hadoop安装环境下的/share/dc/hadoop/api下有自带的api链接,可以方便大家查看,学习。

wordcount:

package hadoop1;import java.io.IOException;import java.util.StringTokenizer;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.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 class TokenizerMapper        extends Mapper{        private final static IntWritable one = new IntWritable(1);    private Text word = new Text();          public void map(Object key, Text value, Context context                    ) throws IOException, InterruptedException {            //split into words by blank just like "//s" in  Regular Expression      StringTokenizer itr = new StringTokenizer(value.toString());      while (itr.hasMoreTokens()) {        word.set(itr.nextToken());        //format :,      context.write() is the format in map&&reduce to output        context.write(word, one);      }    }  }  public static class IntSumReducer        extends Reducer {    private IntWritable result = new IntWritable();    public void reduce(Text key, Iterable values,                        Context context                       ) throws IOException, InterruptedException {      int sum = 0;      //foreach value(a kind of word),      //do sum : ,......,------>, ,......,------>      for (IntWritableval : values) {        sum += val.get();      }      result.set(sum);      //format ,......      context.write(key, result);    }  }  public static void main(String[] args) throws Exception {    Configuration conf = new Configuration();    //get in/out path, format:otherArgs ={input1,input2......,output}    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();    if (otherArgs.length < 2) {      System.err.println("Usage: wordcount  [...] ");      System.exit(2);    }    //singleton    Job job = Job.getInstance(conf, "word count");    job.setJarByClass(WordCount.class);    job.setMapperClass(TokenizerMapper.class);        //combiner is just like a reducer on single node to reduce the net pressure,           //not all the task suit for combiner.    //so ,......,------>    job.setCombinerClass(IntSumReducer.class);    job.setReducerClass(IntSumReducer.class);        //mapper's output    job.setOutputKeyClass(Text.class);    job.setOutputValueClass(IntWritable.class);        //add new path for multinput (input1,input2...)    for (int i = 0; i < otherArgs.length - 1; ++i) {      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));    }    FileOutputFormat.setOutputPath(job,      new Path(otherArgs[otherArgs.length - 1]));    //0 for normal exit,else not normal    System.exit(job.waitForCompletion(true) ? 0 : 1);  }}



wordmean:

package hadoop2;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.FileSystem;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.output.FileOutputFormat;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;import com.google.common.base.Charsets;public class WordMean extends Configured implements Tool {  private double mean = 0;    private final static Text COUNT = new Text("count");  private final static Text LENGTH = new Text("length");  private final static LongWritable ONE = new LongWritable(1);  /**   * Maps words from line of text into 2 key-value pairs; one key-value pair for   * counting the word, another for counting its length.   */  public static class WordMeanMapper extends      Mapper {    private LongWritable wordLen = new LongWritable();    /**     * Emits 2 key-value pairs for counting the word and its length. Outputs are     * (Text, LongWritable).     *      * @param value     *          This will be a line of text coming in from our input file.     */    public void map(Object key, Text value, Context context)        throws IOException, InterruptedException {      StringTokenizer itr = new StringTokenizer(value.toString());      while (itr.hasMoreTokens()) {        String string = itr.nextToken();        //map into format:word1,word2......,wordn.        //for each token(word) split by blank,         //set two kinds of------><"count",1>and<"length",string.length>        this.wordLen.set(string.length());        context.write(LENGTH, this.wordLen);        context.write(COUNT, ONE);      }    }  }  /**   * Performs integer summation of all the values for each key.   */  public static class WordMeanReducer extends      Reducer {// LongWritable is just like Long in java, //to implement hadoop's own type is for Serialization and Anti serialization    private LongWritable sum = new LongWritable();    /**     * Sums all the individual values within the iterator and writes them to the     * same key.     *      * @param key     *          This will be one of 2 constants: LENGTH_STR or COUNT_STR.     * @param values     *          This will be an iterator of all the values associated with that     *          key.     */    public void reduce(Text key, Iterable values, Context context)        throws IOException, InterruptedException {//for two constants:"count" and "length",//calculate the sum for each constant. :,....., -------> &&,......,----->      int theSum = 0;      for (LongWritableval : values) {        theSum += val.get();      }      sum.set(theSum);      context.write(key, sum);    }  }  /**   * Reads the output file and parses the summation of lengths, and the word   * count, to perform a quick calculation of the mean.   *    * @param path   *          The path to find the output file in. Set in main to the output   *          directory.   * @throws IOException   *           If it cannot access the output directory, we throw an exception.   */  private double readAndCalcMean(Path path, Configuration conf)      throws IOException {                  //read from reducers' output     FileSystem fs = FileSystem.get(conf);    Path file = new Path(path, "part-r-00000");    if (!fs.exists(file))      throw new IOException("Output not found!");    BufferedReader br = null;    // average = total sum(m in reduce) / number of elements(n in reduce);    try {            //BufferedReader is a Decorator to InputStreamReader, /for add the method .readLine() and so on.      br = new BufferedReader(new InputStreamReader(fs.open(file), Charsets.UTF_8));      long count = 0;      long length = 0;      String line;      while ((line = br.readLine()) != null) {                     StringTokenizer st = new StringTokenizer(line);                // grab type ------to spilt "count" and "length"        String type = st.nextToken();             // differentiate        if (type.equals(COUNT.toString())) {          String countLit = st.nextToken();          count = Long.parseLong(countLit);          System.out.println("The count is: " + count );//~ add by author :output total word count n        } else if (type.equals(LENGTH.toString())) {          String lengthLit = st.nextToken();          length = Long.parseLong(lengthLit);          System.out.println("The  length  is: " +  length  );//~ add by author :output total word length m        }      }      double theMean = (((double) length) / ((double) count));      System.out.println("The mean is: " + theMean);      return theMean;    } finally {      if (br != null) {        br.close();      }    }  }  public static void main(String[] args) throws Exception {    ToolRunner.run(new Configuration(), new WordMean(), args);  }  @Override  public int run(String[] args) throws Exception {    if (args.length != 2) {      System.err.println("Usage: wordmean  ");      return 0;    }    Configuration conf = getConf();    Job job = Job.getInstance(conf, "word mean");    job.setJarByClass(WordMean.class);    job.setMapperClass(WordMeanMapper.class);    job.setCombinerClass(WordMeanReducer.class);    job.setReducerClass(WordMeanReducer.class);    job.setOutputKeyClass(Text.class);    job.setOutputValueClass(LongWritable.class);    FileInputFormat.addInputPath(job, new Path(args[0]));    Path outputpath = new Path(args[1]);    FileOutputFormat.setOutputPath(job, outputpath);    boolean result = job.waitForCompletion(true);    mean = readAndCalcMean(outputpath, conf);    return (result ? 0 : 1);  }  /**   * Only valuable after run() called.   *    * @return Returns the mean value.   */  public double getMean() {    return mean;  }}

看完了这篇文章,相信你对"hadoop中wordcount 、wordmean的示例代码"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

代码 示例 开头 篇文章 下有 作者 完了 心得 更多 源码 环境 知识 行业 资讯 资讯频道 链接 频道 分析 学习 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 新华互联网科技 北京路客互联网科技有限公司 互联网科技公司怎么样 中国互联网科技发展 凡科互联网科技 上海致骋互联网科技 卫星互联网科技股票 常德市电业局互联网科技部 互联网科技有哪些公司 昭通互联网科技选哪家 岳阳华臻互联网科技有限公司 搜狐科技互联网 佛山市翊博互联网络科技有限公司 深圳市罗太互联网科技有限公司 互联网数据科技专业 区块链结合互联网科技 互联网方面的科技前沿 智帮互联网科技公司最近动向 青岛易易互联网科技 宁波允成互联网科技有限公司 互联网科技股 互联网金融科技委员会 互联网科技创新企业排名 常德市电业局互联网科技部 宜昌元轩互联网科技有限公司 朱啸虎曾投资哪些互联网科技企业 互联网金融科技名词解释 山东星卡互联网科技有限公司官网 广州富喜互联网络科技 下一代互联网重大应用科技园
0