MapReduce 编程模板
1.MapReduce 编程模型的5个步骤:
1)迭代,将输入数据解析成 key/value 对;
2)将解析的 key/value经过Map处理映射成另一组key/value对;
3)根据key进行分组;
4)以分组为单位进行归约(Reduce 过程);
5)迭代,输出最终结果。
2.MapReduce编程模型模板:
在进行编程过程只需改变Map()和Reduce()方法,如果没有Reduce过程时需要对run()作适当调整。
import java.io.IOException;
import java.util.jar.JarException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.jasper.compiler.JavacErrorDetail; public class Example extends Configured implements Tool{ enum Counter{
LINESKIP; //输出错误行
} /*MapClass
* Mapper<
* LongWritable 输入的 key
* Text 输入的 value
* NullWritable/Text 输出的 key
* Text 输出的 value
* >
* */
//public static class Map extends Mapper<LongWritable,Text,NullWritable,Text> //没有Reduce过程时
public static class Map extends Mapper<LongWritable,Text,Text,Text>{
public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException
{
String mydata=value.toString(); //读取源数据
try{
//数据处理
String[] mydataSplite=mydata.split(""); //数据切分
String aData=mydataSplite[0];
String bData=mydataSplite[1]; /*
* 没有Reduce过程时
* Text outText=new Text(aData+""+bData);
* context.write(new Text(aData), new Text(bData)); //输出 key/value ,NullWritable.get()避免输出制表符
*/ context.write(new Text(aData), new Text(bData)); //输出 key/value
}catch(java.lang.ArrayIndexOutOfBoundsException e)
{
context.getCounter(Counter.LINESKIP).increment(1); //出错计数+1
return;
}
}
} /*Reduce静态类
* Reducer<
* Text, 输入的 key
* Text, 输入的 value
* Text, 输出的 key
* Text 输出的 value
*
* Reduce 的输入格式应与 Map的输出格式一致
* >
* */
public static class Reduce extends Reducer<Text,Text,Text,Text>
{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException
{
String valuString;
String outString="";
for (Text value : values)
{
valuString=value.toString();
outString+=valuString+",";
}
context.write(key, new Text(outString)); //输出参数与定义格式一致,如果不是制表符分离的要换成空值
}
} /*run设置运行任务*/
public int run(String[] args) throws Exception {
Configuration conf = getConf(); Job job = new Job(conf,"Example"); //作务名
job.setJarByClass(Example.class); //选择class FileInputFormat.setInputPaths(job, new Path(args[0])); //输入路径
FileOutputFormat.setOutputPath(job, new Path(args[1])); //输出路径 job.setMapperClass(Map.class); //调用 Map class启动Map task
job.setReducerClass(Reduce.class); //调用 Reduce class 雇用 Reduce task
job.setCombinerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class); //输入格式
job.setOutputFormatClass(TextOutputFormat.class); //输出格式
//job.setOutputKeyClass(NullWritable.class); //没有Reduce过程时,输出 key 格式,应该与指定的格式一致
job.setOutputKeyClass(Text.class); //输出 key 格式,应该与指定的格式一致
job.setOutputValueClass(Text.class); //输出 value 格式 System.exit(job.waitForCompletion(true)?0:1);
return 0;
} /*主函数入口*/
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(),new Example(),args);
System.exit(res);
} }
MapReduce 编程模板的更多相关文章
- 批处理引擎MapReduce编程模型
批处理引擎MapReduce编程模型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MapReduce是一个经典的分布式批处理计算引擎,被广泛应用于搜索引擎索引构建,大规模数据处理 ...
- 三、MapReduce编程实例
前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...
- Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)
不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...
- [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...
- mapreduce编程模型你知道多少?
上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- 《Data-Intensive Text Processing with mapReduce》读书笔记之二:mapreduce编程、框架及运行
搜狐视频的屌丝男士第二季大结局了,惊现波多野老师,怀揣着无比鸡冻的心情啊,可惜随着剧情的推进发展,并没有出现期待中的屌丝奇遇,大鹏还是没敢冲破尺度的界线.想百度些种子吧,又不想让电脑留下污点证据,要知 ...
- AJAX编程模板
AJAX一直以来没怎么接触,主要是做JSON数据在服务器和客户端之间传递的时候,被玩坏了,对它莫名的不可爱,最近心理阴影小了,于是又来看看它....... AJAX即“Asynchronous Jav ...
- MapReduce 编程模型
一.简单介绍 1.MapReduce 应用广泛的原因之中的一个在于它的易用性.它提供了一个因高度抽象化而变得异常简单的编程模型. 2.从MapReduce 自身的命名特点能够看出,MapReduce ...
随机推荐
- 【题解】SDOI2011消耗战
虚树模板题~洛谷P2495 第一次写虚树,感觉好厉害呀~首先,这道题目的树形dp是非常显然的,要控制一个点&其子树所有点,要么在子树内部割边,要么直接切点该点与父亲的连边.所以dp[u]表示控 ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 用原生JavaScript做个简单的回到顶部
很多网页在下方都会放置一个“返回顶部”按钮,尤其是页面底部没有导航的网页,这样可以帮助访客重新找到导航或者重温一遍广告(想得真美).随着近几年来 JavaScript 的应用日渐广泛,滑动效果无处不在 ...
- Ecplise下设置jQuery和ExtJs自动提示
Spket 1.6.23下载: http://yunpan.cn/cjJYmEcMFIuuN 访问密码 5642 ext jsb下载:http://yunpan.cn/cjJYR7ZTzibQn ...
- Prepare and Deploy Windows Server 2016 Active Directory Federation Services
https://docs.microsoft.com/en-us/windows/security/identity-protection/hello-for-business/hello-key-t ...
- 移动端list布局,左边固定,右边自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 【uva11468-Substring】AC自动机+dp
http://acm.hust.edu.cn/vjudge/problem/31655 题意:给定k个模板串,n个字符以及选择它的概率pro[i],要构造一个长度问L的字符串s,问s不包含任意一个模板 ...
- 【Foreign】阅读 [线段树][DP]
阅读 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 0 10 4 10 2 3 10 8 ...
- #error#storyboard#xib#解决方案
https://www.evernote.com/shard/s227/sh/cad7d5f5-8e81-4b3b-908f-5d8eee7d11e2/928786149cf9a103a74626 ...
- 【洛谷 P1070】道路游戏 (DP)
题目链接 这题还是很好想的,看到\(90%\)的数据点时,我就知道要用\(n^3\)的算法(最后10分就算了吧) 然后,数据水,直接暴力\(n^3\)卡过了. 显然是道DP. 设\(f[i]\)表示第 ...