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. [Js]表格排序

    思路:遍历每个li,并把它们存放到数组中去,然后通过sort()方法进行排序,再插入 <body>    <input type="button" value=& ...

  2. 二模 (4)day2

    第一题: 题目大意:给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. N<=100000 1.考虑到一个符合要求的连 ...

  3. C# Lodop实现打印

    项目的Debug文件夹下有个template文件夹,里面有用到的js.自己建的要打印的网页和用到的背景图 1.打印方法: class print { public void printzb(strin ...

  4. C# JObject解析Json(多方法解析Json 二)

    下载Newtonsoft.Json,添加引用 记得using Newtonsoft.Json.Linq; //用JObject解析 string json = "{\"offlin ...

  5. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  6. jquery判断点击事件是否为指定区域

    <script type="text/javascript"> $(document).click(function(e){ e = window.event || e ...

  7. System.Web.HttpRequestBase转HttpWebRequest

    /// <summary> /// Copies all headers and content (except the URL) from an incoming to an outgo ...

  8. Ajax返回类型JSON,XML

    Ajax的三种返回类型 **一.TEXT *二.JSON 数据显示页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  9. 《同一个类中不同方法之间的调用相关问题(省略的类名或者this)》

    //同一个类中不同方法之间的调用相关问题(省略的类名或者this) class A { public void B() { System.out.println("b方法运行"); ...

  10. Typographical Concepts

    Glyph(字形) A glyph is an element of writing: an individual mark on a written medium that contributes ...