基础MapReduce程序骨架
来源:互联网 发布:淘宝红包活动名称 编辑:程序博客网 时间:2024/04/27 20:18
/*************************************************** * MapReduce Basic Template * Author: jokes000 * Date: 2011-12-14 * Version: 1.0.0 **************************************************/public class MapReduceTemplate extends Configured implements Tool {public static class MapClass extends Mapper<Object,Object,Object,Object> {// Map Methodpublic void map(KEYIN key, VALUEIN value, Mapper.Context context) {}}public static class Reduce extends Reducer<Object,Object,Object,Object> {// Reduce Methodpublic void reduce(KEYIN key, Iterable<VALUEIN> values, Reducer.Context context) {}}// run Methodpublic int run(String[] args) throws Exception {// Create and Run the JobJob job = new Job();job.setJarByClass(MyJob.class);FileInputFormat.addInputPath(job, new Path(arg0[0]));FileOutputFormat.setOutputPath(job, new Path(arg0[1]));job.setMapperClass(MapClass.class);job.setReducerClass(Reduce.class);job.setInputFormatClass(MyInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);System.exit(job.waitForCompletion(true) ? 0 : 1);return 0;}public static void main(String[] args) throws Exception {// }}
Mapper、Reducer在Job执行期间被多个节点clone以及执行,而剩下的Job Class值在客户端上执行。
除去Mapper和Reducer之后,我们程序的骨架为:
/*************************************************** * MapReduce Basic Template * Author: jokes000 * Date: 2011-12-14 * Version: 1.0.0 **************************************************/public class MapReduceTemplate extends Configured implements Tool {// run Methodpublic int run(String[] args) throws Exception {// Create and Run the JobJob job = new Job();job.setJarByClass(MyJob.class);FileInputFormat.addInputPath(job, new Path(arg0[0]));FileOutputFormat.setOutputPath(job, new Path(arg0[1]));job.setMapperClass(MapClass.class);job.setReducerClass(Reduce.class);job.setInputFormatClass(MyInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);System.exit(job.waitForCompletion(true) ? 0 : 1);return 0;}public static void main(String[] args) throws Exception {// }}这个骨架的核心是run()函数,被成为程序的driver,driver实例化、配置一个Job实体,并使其运行。
Job class会和JobTracker通信使其在集群中执行MapReduce Job。
run()函数中的基础步骤为:
1.创建一个新的Job实例;
2.设置实例运行的主类;
3.设置程序输入输出位置;
4.设置Mapper、Reducer类
5.设置Input、OutputFormat,设置OutputKey、Value类
6.执行Job
Job变量有许多变量,当用户从命令行执行一个Hadoop Job时,他可能想传入一些额外的参数来改变Job的配置。
于是Hadoop提供Configured和Tool类来支持这些参数的识别(具体识别的类为GenericOptionsParser)。
例如:
标准运行命令:bin/hadoop jar MyJob.jar MyJob input/cite77_99.txt output
但是此时我们只想看Mapper的输出(用来Debug),这是我们可以添加这些参数
bin/haoop jar MyJob.jar MyJob -D maped.reduce.tasks=0 input/cite75_99.txt output
GenericOptionsParser支持以下的选项:
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|jobtracker:port> specify a job tracker
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.
- 基础MapReduce程序骨架
- 编写MapReduce基础程序
- Blender 基础 骨架 01
- iPhone 程序骨架浅析
- Blender 基础 骨架-02 骨架的各种呈现方式
- 微信小程序-初识小程序架构骨架
- MapReduce基础
- MapReduce基础
- MapReduce基础
- MapReduce基础
- MapReduce基础
- 使用Seam-gen生成基础项目骨架
- Hadoop inAction] 第4章 编写MapReduce基础程序
- 虚幻UE4中Matinee基础:骨架网格教程
- android电商实战note二 基础骨架搭建
- 邮票管理的小程序骨架写完了
- usb-skeleton.c 之00----起步usb骨架程序!!!
- scala java+spring boot写spark程序骨架
- mysql查询优化-:未优化前的效果
- 读者写者问题--使用信号量的读者优先与写者优先程序分析
- eclipse cdt 无法正确显示代码提示 No Default Proposals
- charsequence转为string
- ADO.net学习笔记
- 基础MapReduce程序骨架
- 简单标签开实现类似QQ搜搜的分页效果
- git的使用
- 构建自己的Android账户与内容同步机制,例程SampleSyncAdapter的分析
- Linux下Makefile的automake生成全攻略
- C/S模式与B/S模式的比较分析
- xcode info.plist讲解
- JTable常见用法细则
- instanceof