package wordcount;
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<Object, Text, Text, IntWritable>{   //继承泛型类Mapper
               
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():返回从当前位置到下一个分隔符的字符串,word.set():Java数据类型与hadoop数据类型转换
                                context.write(word, one);   //hadoop全局类context输出函数write;
                        }
        
}

}

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {    //继承泛型类Reducer
        
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();
                    result.set(sum);   //Java数据类型sum,转换为hadoop数据类型result
                    context.write(key, result);   //输出结果到hdfs
         
}
}

public static void main(String[] args) throws Exception {
        
Configuration conf = new Configuration();   //实例化Configuration
/***********
GenericOptionsParser是hadoop框架中解析命令行参数的基本类。 getRemainingArgs();返回数组【一组路径】
*********/
/**********
函数实现
public String[] getRemainingArgs() {
    return (commandLine == null) ? new String[]{} : commandLine.getArgs();
  }

/********
//总结上面:返回数组【一组路径】
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

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

Job job = Job.getInstance(conf, "word count");   //实例化job
job.setJarByClass(wordcount.class);   //为了能够找到wordcount这个类
job.setMapperClass(TokenizerMapper.class);   //指定map类型
/********
指定CombinerClass类
这里很多人对CombinerClass不理解
************/
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);  //指定reduce类
job.setOutputKeyClass(Text.class); //rduce输出Key的类型,是Text
job.setOutputValueClass(IntWritable.class);  // rduce输出Value的类型

for (int i = 0; i < otherArgs.length - 1; ++i)
   FileInputFormat.addInputPath(job, new Path(otherArgs));  //添加输入路径

FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));   //添加输出路径
System.exit(job.waitForCompletion(true) ? 0 : 1);  //提交job
}
}

wordcount源代码详解的更多相关文章

  1. Hadoop集群WordCount运行详解(转)

    原文链接:Hadoop集群(第6期)_WordCount运行详解 1.MapReduce理论简介 1.1 MapReduce编程模型 MapReduce采用"分而治之"的思想,把对 ...

  2. Mpg123源代码详解

    Mpg123与libmad一样,支持mpeg1,2,2.5音频解码.目前来看mpg123比libmad支持了网络播放功能.而且libmad基本上开源社区在2005年左右,基本停止更新,mpg123至今 ...

  3. WordCount运行详解

    1.MapReduce理论简介 1.1 MapReduce编程模型 MapReduce采用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完成,然 ...

  4. hadoop WordCount例子详解。

    [学习笔记] 下载hadoop-2.7.4-src.tar.gz,拷贝hadoop-2.7.4-src.tar.gz中hadoop-mapreduce-project\hadoop-mapreduce ...

  5. Hadoop下面WordCount运行详解

    单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为MapReduce版"Hello World",该程序的完整代码可以在Hadoop安装包的"src/ ...

  6. 结合源代码详解android消息模型

    Handler是整个消息系统的核心,是Handler向MessageQueue发送的Message,最后Looper也是把消息通知给Handler,所以就从Handler讲起. 一.Handler H ...

  7. mapreduce入门之wordcount注释详解

    mapreduce版本:0.2.0之前 说明: 该注释为之前学习时找到的一篇,现在只是在入门以后对该注释做了一些修正以及添加. 由于版本问题,该代码并没有在集群环境中运行,只将其做为理解mapredu ...

  8. java Object类源代码详解 及native (转自 http://blog.csdn.net/sjw890821sjw/article/details/8058843)

    package java.lang; public class Object { /* 一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用.*/ private static na ...

  9. 【算法】C++用链表实现一个箱子排序附源代码详解

    01 箱子排序 1.1 什么是分配排序? 分配排序的基本思想:排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O ...

随机推荐

  1. .net aop 操作 切面应用 Castle.Windsor框架 spring 可根据接口 自动生成一个空的实现接口的类

    通过unget 安装Castle.Windsor using Castle.DynamicProxy; using System; using System.Collections.Generic; ...

  2. idea工具的快捷方式

    用idea默认的快捷键 Ctrl+~,快速切换方案(界面外观.代码风格.快捷键映射等菜单) Shift+Enter,向下插入新行 Ctrl+F,查找文本 Ctrl+R,替换文本 Ctrl+I,实现方法 ...

  3. python3简单实现支持括号的加减乘除运算

    1.首先表达式的空格. 2.循环计算最内层带括号的表达式(提取运算符出现的顺序,然后计算) 3.计算最外层的表达式输出. 刚接触python,代码不够严谨,仅实现功能.不知道如何在函数中使用运算符变量 ...

  4. 看我如何粘贴别人代码--socketserver

    源码执行流程 自己模仿一个(提取代码) 服务器类 import socket import threading import selectors class TCPServer: def __init ...

  5. loongson 2f 和u-boot中的cache命令对照

    00000 Index Invalidate INDEX_INVALIDATE_I (I) 00001 Index WriteBack Invalidate INDEX_WRITEBACK_INV_D ...

  6. MQTT初步使用

    环境搭建 1.mosquitto所需要的rpm包 2.c-ares-1.12.0 3.安装最新的openssl版本 4.mosquitto-1.4.10 mosquitto需要的rpm包 c-ares ...

  7. Win10 中将网页转换成pdf的简便方法

    注意:该方法不是将网页完整地保存下来,而是选取其中主要的文字信息. (1)打开要保存的网页 (2)按快捷键 Ctrl+P 打开打印界面 (3)选择打印机为 “Microsoft Print to PD ...

  8. python之地基(一)

    想要建起一座高楼,最重要的就是建一个扎实地基,以下的内容就是地基的一部分,往你用心去阅读,去练习,去掌握. 一.变量 变量是什么?什么是变量?变量有什么好处? 变量是一种使用方便的占位符,用于引用计算 ...

  9. xpath定位动态iframe

    使用xpath定位 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[starts-with(@id, 'x-URS ...

  10. python实现常见排序算法

    #coding=utf-8from collections import deque #冒泡排序def bubblesort(l):#复杂度平均O(n*2) 最优O(n) 最坏O(n*2) for i ...