mapreduce版本:0.2.0之前

说明:  

  该注释为之前学习时找到的一篇,现在只是在入门以后对该注释做了一些修正以及添加。

  由于版本问题,该代码并没有在集群环境中运行,只将其做为理解mapreduce的参考吧。

  切记,该版本是0.2.0之前的版本,请分辨清楚!

正文:

  

package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
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.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat; public class WordCount
{
//Map类继承自MapReduceBase,并且实现了Mapper接口,此接口是一个规范类型.
//它有4种形式的参数,分别用来指定map的输入key、value值类型,输出key、value值类型
public static class Map
extends MapReduceBase
implements Mapper<LongWritable, Text, Text, IntWritable>
{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); //实现map方法,对输入值进行处理。(此处用来去掉空格)
public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException
{
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens())
{
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
} /*
//Reduce类也是继承自MapReduceBase的,需要实现Reducer接口。
//Reduce类以map的输出作为输入,因此Reduce的输入类型是<Text,Intwritable>。
//而Reduce的输出是单词和它的数目,因此,它的输出类型是<Text,IntWritable>。
//Reduce类也要实现reduce方法,在此方法中,reduce函数将输入的key值作为输出的key值,然后将获得多个value值加起来,作为输出的值。
*/
public static class Reduce
extends MapReduceBase
implements Reducer<Text, IntWritable, Text, IntWritable>
{
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException
{
int sum = 0;
while (values.hasNext())
{
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
} public static void main(String[] args) throws Exception
{ //1.用JobConf类对 MapReduce job进行初始化
JobConf conf = new JobConf(WordCount.class);
// 调用setJobName()方法命名这个Job
conf.setJobName("wordcount"); //setup2:设置Job输出结果<key,value>的中key和value数据类型,因为结果是<单词,个数>
//所以key设置为"Text"类型,相当于Java中String类型。
conf.setOutputKeyClass(Text.class);
//Value设置为"IntWritable",相当于Java中的int类型。
conf.setOutputValueClass(IntWritable.class); //setup3:指定job的MapReduce,以及combiner
//设置Job处理的Map(拆分)
conf.setMapperClass(Map.class);
//设置Job处理的Combiner(中间结果合并,这里用Reduce类来进行Map产生的中间结果合并,避免给网络数据传输产生压力。)
也可以不用设置(已默认)
conf.setCombinerClass(Reduce.class);
//设置Job处理的Reduce(合并)
conf.setReducerClass(Reduce.class); //指定输入输出路径,可在项目上右键->Run As->Run Configuration->arguments->program arguments中配置
即为main(String[] args)中String[] args赋值
//指定InputPaths
eg:hdfs://master:9000/input1/
FileInputFormat.setInputPaths(conf, new Path(args[0]));
//指定outputPaths
eg:hdfs://master:9000/input1/
FileOutputFormat.setOutputPath(conf, new Path(args[1])); JobClient.runJob(conf);
}
}

  

mapreduce入门之wordcount注释详解的更多相关文章

  1. JScript中的条件注释详解(转载自网络)

    JScript中的条件注释详解-转载 这篇文章主要介绍了JScript中的条件注释详解,本文讲解了@cc_on.@if.@set.@_win32.@_win16.@_mac等条件注释语句及可用于条件编 ...

  2. 大数据Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解

    微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...

  3. Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解

    转自:http://blog.csdn.net/iamdll/article/details/20998035 分类: 分布式 2014-03-11 10:31 156人阅读 评论(0) 收藏 举报 ...

  4. Spring 入门 web.xml配置详解

    Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...

  5. 爬虫入门之urllib库详解(二)

    爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...

  6. 《挑战30天C++入门极限》入门教程:实例详解C++友元

        入门教程:实例详解C++友元 在说明什么是友元之前,我们先说明一下为什么需要友元与友元的缺点: 通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为pu ...

  7. MapReduce On Yarn的配置详解和日常维护

    MapReduce On Yarn的配置详解和日常维护 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MapReduce运维概述 MapReduce on YARN的运维主要是 ...

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

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

  9. MapReduce 1工作原理图文详解

    MapReduce工作原理图文详解 一 MapReduce程序执行流程 程序执行流程图如下: 流程分析:1.在客户端启动一个作业.2.向JobTracker请求一个Job ID.3.将运行作业所需要的 ...

随机推荐

  1. hdu 4628 Pieces

    http://acm.hdu.edu.cn/showproblem.php?pid=4628 状态压缩DP 时间复杂度应该是 16*(2^32) 但是运行时要远小于这个数 所以加一定剪枝就可以过 代码 ...

  2. ARC 类型转换:显式转换 id 和 void *

    http://blog.csdn.net/chinahaerbin/article/details/9471419 /* * ARC有效时三种类型转换: */ 1.__bridge           ...

  3. 如何判断Intent有没有对应的Activity去处理?

    如何判断Intent有没有对应的Activity去处理?至少有以下两种方法,最近使用过,随笔记下来,以供查阅. 第一种, 如下: public boolean isIntentResolvable(I ...

  4. json数组,随便测试

    Pid := '1001411225514227,926792194654225'; json := SA([]); json.AsArray.Add(SO(pid)); ShowMessage( j ...

  5. IP数据报的格式

    1. IP数据报首部的固定部分中的各字段 ①版本:占4位,指IP协议的版本.通信双方使用的 IP协议版本必须一致.日前广泛使用的 IP协议版本号为 4 (即 IPv4). IPv6 目前还处于起步阶段 ...

  6. Redhat6.x下如何进行远程安装虚拟机

    远程主机IP:192.168.122.1 远程主机名:server1.example.com 本地主机IP:192.168.122.2 本地主机名:server2.example.com 1.登录到远 ...

  7. .net异常小总

    1.  ExecuteReader:CommandText属性尚未初始化 即:没有对sqlCommand对象的CommandText属性赋值,即没有写sql语句. 2.  由于代码已经过优化或者本机框 ...

  8. VMware-workstation-full-10.0.1-1379776 CN

    从V10版本开始,VMware Workstation 官方自带简体中文了,以后大家不需要汉化啦! 今天,VMware Workstation 10.0.1正式发布,版本号为Build 1379776 ...

  9. hdoj-2033

    A+B系列: #include "stdio.h"int main(){ int a[3],b[3],c[3],i,n,j,flag; while(~scanf("%d& ...

  10. C++中的数组与指针

    数组与指针看起来很像 int a[] = {1, 2 ,3}; int *p = a; 如此,我们可以p[0], p[1], p[2] 看起来,与直接使用数组名没什么两样,但是看这段代码 sizeof ...