写MapReduce程序的步骤:

  1. 把问题转化为MapReduce模型;
  2. 设置运行参数;
  3. 写map类;
  4. 写reduce类;

例子:统计单词个数

Map的任务是将内容用“ ”分开,然后每个都对应1,Reduce将相同的统计起来

1,Map端:一行行读文件,程序转化为中间Key/Value。每一个键值对调用一次map函数。

hello you hello me → hello 1,you 1,hello 1,me 1;

2,Reduce端:相同的Key肯定会在一起。经过Reduce方法处理后形成最终的Key/Value

hello 1,hello 1→hello 2;

写一个MapClass extends Mapper<keyin,valuein,keyout,valueout>类,实现map方法;

用java思想理解:

	//word.txt 内容(两行)
	//"hello you hello world	→hello 1, you 1,hello 1,world 1
	//hello me";				→hello 1,me 1	

	String str = "hello you hello world";
	//String[] strs=str.split(" ");//按空格划分
	//strs[0]=hello →map(key,1) →map(hello,1)

用MapReduce实现:

package mapreduce;

import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MapClass extends Mapper<Object, Text, Text, IntWritable> {
	//Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
	//参数:Object,Text(和java的String一样),Text,IntWritable(和java int一样)
	//Map的输出是Reduce的输入;

	public Text keyText = new Text("key");//相当于String ketText="key"
	public IntWritable intValue = new IntWritable(1);
	protected void map(Object key, Text value, Context context) throws java.io.IOException, InterruptedException {
		//获取值
		String str = value.toString();
		//默认空格分割
		StringTokenizer stringToKenizer = new StringTokenizer(str);

		while (stringToKenizer.hasMoreTokens()) {
			keyText.set(stringToKenizer.nextToken());
			context.write(keyText, intValue);//context.write("My",1) //上下文
		}
	};

}

一个ReduceClass  extends Reducer< keyin,valuein,keyout,valueout >类;实现reduce方法:

package mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
import org.apache.hadoop.mapreduce.Reducer;

public class ReduceClass extends Reducer<Text, IntWritable, Text, IntWritable> {
	//Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
	//Map的输出是Reduce的输入;

	public IntWritable intValue = new IntWritable(1);

	protected void reduce(Text key, java.lang.Iterable<IntWritable> values,//name [1,1]
			org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws java.io.IOException, InterruptedException {
		int sum = 0;
		while (values.iterator().hasNext()) {
			sum += values.iterator().next().get();
		}
		intValue.set(sum);
		context.write(key, intValue);//上下文

	};

}

接下来写main测试,新建一个类WordCounter(其中的main拷贝源码例子中的main如下:

hadoop-1.1.2\src\examples\org\apache\hadoop\examples\WordCount.java

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2);
    }
    Job job = new Job(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }

WordCounter类

package mapreduce;

import java.io.IOException;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCounter {
	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}
		Job job = new Job(conf, "word count");
		job.setJarByClass(WordCounter.class);//打包jar要写,执行的类:本类
		job.setMapperClass(MapClass.class);//MapClass类
		//job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(ReduceClass.class);//ReduceClass类
		job.setOutputKeyClass(Text.class);//输出的key类型
		job.setOutputValueClass(IntWritable.class);//输出的value类型
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//输入参数
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//输出参数
		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}
}

完成后导出成jar包放到hadoop运行:

右击包名,Export,Java,JAR file 导出,拷贝到Linux桌面;

上传到hadoop: 记得打开java进程:start-all.sh

[root@hadoop Desktop]# hadoop fs -put mapreduceTest.jar /

新建一个文件:

root@hadoop Desktop]# vi wordTest.txt

内容:

hello you
hello me
hello world

将文件上传到hadoop:

[root@hadoop Desktop]# hadoop fs -put wordTest.txt /

运行:参数4:包名加类名;5上一步上传到的文件,6输出到哪里

[root@hadoop Desktop]# hadoop jar mapreduceTest.jar cn.mapreduce.WordCounter /wordTest.txt /outputTest

查看日志:(/part-r-00000是固定的)

[root@hadoop Desktop]# hadoop fs -text /outputTest/part-r-00000
Warning: $HADOOP_HOME is deprecated.

hello	3
me	1
world	1
you	1

完成;

通过一个例子了解MapReduce的更多相关文章

  1. Hadoop学习历程(四、运行一个真正的MapReduce程序)

    上次的程序只是操作文件系统,本次运行一个真正的MapReduce程序. 运行的是官方提供的例子程序wordcount,这个例子类似其他程序的hello world. 1. 首先确认启动的正常:运行 s ...

  2. spring笔记--使用springAPI以及自定义类 实现AOP的一个例子

    Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...

  3. ReCap 360 photo照片建模技术的又一个例子

    这是我做的又一个利用Autodesk ReCap 360 照片建模技术做的一个例子.你可以下载模型自己把玩,或者下载原始照片自己试一试. 拍摄工具: 小米手机 照片数量:约120张 后期处理工具: p ...

  4. 从一个例子中体会React的基本面

    [起初的准备工作] npm init npm install --save react react-dom npm install --save-dev html-webpack-plugin web ...

  5. 用thinkphp写的一个例子:抓取网站的内容并且保存到本地

    我需要写这么一个例子,到电子课本网下载一本电子书. 电子课本网的电子书,是把书的每一页当成一个图片,然后一本书就是有很多张图片,我需要批量的进行下载图片操作. 下面是代码部分: public func ...

  6. Erlang 程序引发共享内存 bug 的一个例子

    虽然 Erlang 的广告说得非常好,functional.share-nothing.消息传递,blah blah 的,好像用 Erlang 写并发程序就高枕无忧了,但是由于 Erlang 信奉高度 ...

  7. 对Jena的简单理解和一个例子

    本文简单介绍Jena(Jena 2.4),使用Protégé 3.1(不是最新版本)创建一个简单的生物(Creature)本体,然后参照Jena文档中的一个例子对本体进行简单的处理,输出本体中的Cla ...

  8. 使用flume的一个例子

    新项目中需要使用到hadoop和vertica,使用flume把数据加载到hadoop中,我做了一个例子, 即监控一个sharefolder,如果里面有文件,则会文件load到hadoop. 开启Fl ...

  9. php部分--面向对象三大特性-封装(另加连续调用的一个例子)、继承(重写、重载的例子)、多态;

    一.封装性: 目的:为了使类更加安全. 做法:1设置私有成员 2在类中建方法,访问私有成员 3在方法里边加控制(if) 私有成员访问的两种方法: 方法一:set(可写) get(可读)做方法(可读可写 ...

随机推荐

  1. 处处留心皆学问——由“display:inline-block;”导致的间距引发的思考。

    昨天在做一个demo时遇到了一个问题:我有五个li需要并排排列,然后自然而然的我给它们设了display:inline-block;但是,过了很久之后发现,除了我写的样式外,它默认有一个间距,我们都不 ...

  2. [HNOI 2011]数矩形

    Description 题库链接 给出平面上 \(n\) 个点,选出四个点作为矩形顶点.求出矩形最大面积. \(1\leq n\leq 1500\) Solution 转载自 Z-Y-Y-S dark ...

  3. 51 nod 1681 公共祖先 (主席树+dfs序)

    1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...

  4. 51nod 1270 数组的最大代价

    1270 数组的最大代价题目来源: HackerRank基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 数组A包含N个元素A1, A2......AN.数组B包含N ...

  5. 【BZOJ3506】【Cqoi2014】排序机械臂

    传送门(因为BZOJ上没有题面...所以放的是luogu的) 题意:你需要维护一个序列,支持区间翻转与查询区间最小. 解题思路:由于区间最小实际上每一次就是对应的整个数列的第k小,因此可以直接预处理解 ...

  6. 【网络流】【BZOJ1070】【SCOI2007】修车

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1070 题意:问你如何分配老司机使得每部车的等待时间之和最短. 解题思路:本题不易正做,考虑 ...

  7. ●BZOJ 4361 isn

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4361 题解: 容斥,DP,树状数组 注意题意:一旦变成了非降序列,就停止操作.即对非降序列进 ...

  8. *hdu 5536(字典树的运用)

    Input The first line of input contains an integer T indicating the total number of test cases. The f ...

  9. [BZOJ]3672 购票(Noi2014)

    革命尚未成功,同志还需努力. Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ ...

  10. ubuntu 16.04 安装 tensorflow-gpu 包括 CUDA ,CUDNN,CONDA

    ubuntu 16.04 安装 tensorflow-gpu 包括 CUDA ,CUDNN,CONDA 显卡驱动装好了,如图: 英文原文链接: https://github.com/williamFa ...