第一个MapReduce程序
计算文件中每个单词的频数
wordcount 程序调用 wordmap 和 wordreduce 程序。
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.output.TextOutputFormat; public class wordcount { /**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub Configuration conf = new Configuration();
Job job = new Job(conf,"wordcount");
job.setJarByClass(wordcount.class); job.setMapperClass(wordmap.class);
job.setReducerClass(wordreduce.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.waitForCompletion(true); } }
wordmap 程序的输入为<key,value>(key是当前输入的行数,value对应的是行的内容),然后对此行的内容进行切词,每切下一个词就将其组织成<word,1>的形式,word表示文本内容,1代表出现了一次。
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class wordmap extends Mapper<LongWritable, Text, Text, IntWritable> { private static final IntWritable one = new IntWritable(1);
protected void map(
LongWritable key,
Text value,
org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws java.io.IOException, InterruptedException { String line = value.toString();
String[] words = line.split(" ");
for(String word : words){
context.write(new Text(word), one); } }; }
wordreduce 程序会接受到<word,{1,1,1,1……}>形式的数据,也就是特定单词及其出现的次数,其中 "1" 表示 word 出现的频数,所以每接收一个<word,{1,1,1,1……}>,就会在 word 的频数加 1 ,最后组织成<word,sum>的形式直接输出。
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class wordreduce extends Reducer<Text, IntWritable, Text, IntWritable> { protected void reduce(
Text key,
java.lang.Iterable<IntWritable> values,
org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws java.io.IOException, InterruptedException { int sum = 0;
for(IntWritable count : values){
sum+= count.get(); }
context.write(key, new IntWritable(sum));
}; }
第一个MapReduce程序的更多相关文章
- 一起学Hadoop——使用IDEA编写第一个MapReduce程序(Java和Python)
上一篇我们学习了MapReduce的原理,今天我们使用代码来加深对MapReduce原理的理解. wordcount是Hadoop入门的经典例子,我们也不能免俗,也使用这个例子作为学习Hadoop的第 ...
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...
- 编写自已的第一个MapReduce程序
从进入系统学习到现在,貌似我们还没有真正开始动手写程序,估计有些立志成为Hadoop攻城狮的小伙伴们已经有些急了.环境已经搭好,小讲也有些按捺不住了.今天,小讲就和大家一起来动手编写我们的第一个Map ...
- 编写第一个MapReduce程序—— 统计气温
摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...
- 从零开始学习Hadoop--第2章 第一个MapReduce程序
1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...
- 第一个MapReduce程序——WordCount
通常我们在学习一门语言的时候,写的第一个程序就是Hello World.而在学习Hadoop时,我们要写的第一个程序就是词频统计WordCount程序. 一.MapReduce简介 1.1 MapRe ...
- Hadoop学习之第一个MapReduce程序
期望 通过这个mapreduce程序了解mapreduce程序执行的流程,着重从程序解执行的打印信息中提炼出有用信息. 执行前 程序代码 程序代码基本上是<hadoop权威指南>上原封不动 ...
- 运行第一个MapReduce程序,WordCount
1.安装Eclipse 安装后如果无法启动重新配置Java路径(如果之前配置了Java) 2.下载安装eclipse的hadoop插件 注意版本对应,放到/uer/lib/eclipse/plugin ...
- Hadoop 6、第一个mapreduce程序 WordCount
1.程序代码 Map: import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.h ...
随机推荐
- 用Eclipse来开发STM32
先贴一个官方说明文档:http://www.keil.com/support/man/docs/ecluv/default.htm
- ApiDemo/FragmentRetainInstance 解析
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- DB9 公头母头引脚定义及连接
1.实物及引脚简单介绍 在做开发的时候常常会用到串行接口,一般9针的串行接口居多.例如以下图所看到的: 公头母头用于连接线的採用上图封装.但用于开发板的时候採用90度弯角插针的封装.例如以下图: 各引 ...
- Android开发之初识Camera图像采集
/* * Android开发之初识camera图像采集 * 北京Android俱乐部群:167839253 * Created on: 2011-8-24 * Author: blueeagle * ...
- 框架使用的技术主要是SpringMVC 在此基础上进行扩展
框架使用的技术主要是SpringMVC 在此基础上进行扩展 1 Web前端使用 2 前段控制器采用SpringMVC零配置 3 IOC容器Spring 4 ORM使用 Mybites或者hiberna ...
- JVM自动内存管理学习笔记
对于使用 C.C++ 的程序员来说,在内存管理领域,他们既是拥有最高权力的皇帝又是从事最基础工作的劳动人民——拥有每一个对象的“所有权”,又担负着每一个对象生命开始到终结的维护责任.对于 Java 程 ...
- printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换函数
参考:http://blog.sina.com.cn/s/blog_674b5aae0100prv3.html 总览 (SYNOPSIS) #include <stdio.h> int p ...
- 1043. Is It a Binary Search Tree (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- mysql 优化工具
explain profiling 建议提供以下信息 show table status like 'audit';show create table audit;show index from a ...
- BUG: GetDC() ReleaseDC()引起的内存泄漏
调用CWnd::GetDC函数跟CWnd::ReleaseDC函数的代码运行时,会出现 4 个字节的内存泄漏. Microsoft 已经确认这是在本文开头部分列出的 Microsoft 产品中的缺陷. ...