mapreduce代码实现入门
mapreduce代码主要包括三个类,map类、reduce类以及测试类!
以wordcount为例,
map类为:
static class WordMapper extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreElements()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
reduce类为:
static class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable res = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException
{
int sum = 0;
for(IntWritable val:values){
sum += val.get();
}
res.set(sum);
context.write(key, res);
}
}
主函数代码为:
public static void main(String args[]) throws Exception{
String inputfilepath = "hdfs://localhost:9000/input1";
String outputfilepath = "hdfs://localhost:9000/output4";
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setJobName("word-count");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(WordMapper.class);
job.setReducerClass(WordReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(inputfilepath));
FileOutputFormat.setOutputPath(job, new Path(outputfilepath));
job.waitForCompletion(true);
}
其他的hadoop简单实例代码如:
数字求和:
package goal; 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.FloatWritable;
import org.apache.hadoop.io.LongWritable;
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 Sum { public static class SumMapper extends
Mapper<Object, Text, Text, FloatWritable>{
private Text word = new Text("sum");
private static FloatWritable nv = new FloatWritable(1.0f);
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException
{
StringTokenizer str = new StringTokenizer(value.toString());
float sum = 0;
while(str.hasMoreTokens()){
String s = str.nextToken();
float val = Float.parseFloat(s);
sum = val;
}
nv.set(sum);
context.write(word, nv);
}
}
public static class SumReducer extends
Reducer<Text, FloatWritable, Text, FloatWritable>{
private Text k = new Text("sum");
private FloatWritable res = new FloatWritable();
public void reduce(Text key, Iterable<FloatWritable> values,
Context context) throws IOException, InterruptedException{
float sum = 0;
for(FloatWritable val : values){
float v = val.get();
sum += v;
}
res.set(sum);
context.write(k, res);
}
} public static void main(String args[])throws Exception{
String other[] = {"hdfs://localhost:9000/input2/1.txt", "hdfs://localhost:9000/output3"};
Configuration conf = new Configuration();
System.out.println("yes");
Job job = new Job(conf, "number sum");
job.setJarByClass(Sum.class);
job.setMapperClass(SumMapper.class);
job.setReducerClass(SumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FloatWritable.class);
FileInputFormat.addInputPath(job, new Path(other[0]));
FileOutputFormat.setOutputPath(job, new Path(other[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
System.out.println("yes");
} }
mapreduce代码实现入门的更多相关文章
- Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)
不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...
- Hadoop MapReduce编程 API入门系列之挖掘气象数据版本3(九)
不多说,直接上干货! 下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 下面是版本2. Hadoop MapReduce编程 API入门系列之挖掘气象数 ...
- Hadoop MapReduce编程 API入门系列之挖掘气象数据版本2(十)
下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 这篇博文,包括了,实际生产开发非常重要的,单元测试和调试代码.这里不多赘述,直接送上代码. MRUni ...
- Centos下命令行编译MapReduce代码(Java)并打包在Hadoop中执行
前提条件:搭建好Hadoop系统 新建文件夹:input 和 output hdfs dfs -mkdir /inputhdfs dfs -mkdir /output 查看文件系统 hdfs df ...
- 【甘道夫】官方网站MapReduce代码注释具体实例
引言 1.本文不描写叙述MapReduce入门知识,这类知识网上非常多.请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop ...
- 大数据(6) - MapReduce简易介绍入门
一 MapReduce入门 MapReduce定义(简单来说就是hadoop的数据分析核心,理解其中的原理,则可以分析聚合一切需求) Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于 ...
- Hadoop MapReduce编程 API入门系列之薪水统计(三十一)
不多说,直接上代码. 代码 package zhouls.bigdata.myMapReduce.SalaryCount; import java.io.IOException; import jav ...
- Hadoop MapReduce编程 API入门系列之小文件合并(二十九)
不多说,直接上代码. Hadoop 自身提供了几种机制来解决相关的问题,包括HAR,SequeueFile和CombineFileInputFormat. Hadoop 自身提供的几种小文件合并机制 ...
- Hadoop MapReduce编程 API入门系列之mr编程快捷键活用技巧详解(四)
1.Shift + Alt + S Hadoop没有使用jdk自带的默认序列化机制. 现在呢,hadoop-2.*里有两套序列化机制.一个是自己hadoop的序列化机制,一个是谷歌的. 所以,要改为. ...
随机推荐
- Spring Boot学习——AOP编程的简单实现
首先应该明白一点,AOP是一种编程范式,是一种程序设计思想,与具体的计算机编程语言无关,所以不止是Java,像.Net等其他编程语言也有AOP的实现方式.AOP的思想理念就是将通用逻辑从业务逻辑中分离 ...
- 分享C#识别图片上的数字
通过Emgu实现对图片上的数字进行识别.前期步骤:1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持.2.安装后需填写环境变量,环境变量Path值后加入Emgu安装 ...
- 对动态规划(Dynamic Programming)的理解:从穷举开始(转)
转自:http://janfan.cn/chinese/2015/01/21/dynamic-programming.html 动态规划(Dynamic Programming,以下简称dp)是算法设 ...
- Codeforces 371B Fox Dividing Cheese(简单数论)
题目链接 Fox Dividing Cheese 思路:求出两个数a和b的最大公约数g,然后求出a/g,b/g,分别记为c和d. 然后考虑c和d,若c或d中存在不为2,3,5的质因子,则直接输出-1( ...
- Add and Search Word - Data structure design - LeetCode
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- POJ 1155 TELE [树状DP]
题意:略. 思路:用dp[i][k]来表示结点i给k个用户提供节目时的最大盈利(可能为负). 则递推方程为: dp[i][j] = max(dp[i][j], dp[i][m] + dp[v][j-m ...
- API网关服务Zuul-Spring Cloud学习第五天(非原创)
文章大纲 一.Zuul是什么二.Zuul的基本实现三.路由配置细节四.异常处理细节五.项目源码与参考资料下载六.参考文章 一.Zuul是什么 到目前为止,我们Spring Cloud中的内容已 ...
- js 拦截全局 ajax 请求
你是否有过下面的需求:需要给所有ajax请求添加统一签名.需要统计某个接口被请求的次数.需要限制http请求的方法必须为get或post.需要分析别人网络协议等等,那么如何做?想想,如果能够拦截所有a ...
- 【ActiveMQ】1.下载安装启动使用
官网下载:http://activemq.apache.org/activemq-5121-release.html 官网指导文档:http://activemq.apache.org/version ...
- Scut游戏服务器引擎5.6.3.5发布
版本:5.6.3.5 (2013-11-25) 1. 优化实体ChangeKey队列,减少写库IO(默认为5分钟写入一次数据库) 2. 优化Protobuf序列化启用自动GZip压缩,减少Redis内 ...