`

MapReduce编程模板

 
阅读更多

 

 

 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class ModuleMapReduce extends Configured implements Tool {

	static class ModuleMapper extends
			Mapper<LongWritable, Text, Text, IntWritable> {
		@Override
		protected void setup(
				Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.setup(context);
		}

		@Override
		protected void map(LongWritable key, Text value,
				Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.map(key, value, context);
		}

		@Override
		protected void cleanup(
				Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.cleanup(context);
		}

		@Override
		public void run(
				Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.run(context);
		}

	}

	static class ModuleReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		@Override
		protected void setup(
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.setup(context);
		}

		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.reduce(key, values, context);
		}

		@Override
		protected void cleanup(
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.cleanup(context);
		}

		@Override
		public void run(
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			super.run(context);
		}

	}

	@Override
	public int run(String[] args) throws Exception {
		if (checkArgs(args) != 0)
			return checkArgs(args);

		Job job = getJob();
		//set input/output path
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		// set run jar class when package need
		job.setJarByClass(ModuleMapper.class);
		//set map info
		job.setMapperClass(ModuleMapper.class);
		job.setInputFormatClass(TextInputFormat.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(LongWritable.class);
		//set partition
		job.setPartitionerClass(HashPartitioner.class);
		job.setNumReduceTasks(1);
		//set reduce info
		job.setReducerClass(ModuleReducer.class);
		job.setOutputFormatClass(TextOutputFormat.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		return job.waitForCompletion(true) ? 0 : 1;
	}

	private Configuration getConfiguration() {
		Configuration conf = new Configuration();
		return conf;
	}

	private Job getJob() throws IOException {
		Configuration conf = getConfiguration();
		Job job = new Job(conf, ModuleMapReduce.class.getSimpleName());
		return job;
	}

	private int checkArgs(String[] args) {
		if (args.length != 2) {
			System.err.println("args error");
			return 1;
		}
		return 0;
	}

	public static void main(String[] args) throws Exception {
		int exitCode = ToolRunner.run(new ModuleMapReduce(), args);
		System.exit(exitCode);
	}
}

 

分享到:
评论

相关推荐

    mapreduce模板.txt

    MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算。概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数式编程语言里借来的,还有从矢量编程语言里借来的特性。它极大地方便了编程...

    MapReduce设计模式

    MapReduce作为一种分布式海量数据处理的编程框架,已经得到业界的...《MapReduce设计模式》面向中高级MapReduce开发者,涵盖了绝大部分MapReduce编程可能面对的场景,相信初学者和专家同样可以在本书中得到一些启示。

    实验3模板-MapReduce实验

    实验3模板-MapReduce实验 实验报告

    大数据概论课程设计-基于 KNN 与 MapReduce 的电影网站用户性别预测(附课程设计报告)

    2.掌握以 MapReduce 编程实现 KNN 算法 3.掌握以 MapReduce 编程实现 KNN 分类器评价 三、项目分析 什么有个性化的电影推荐服务?举个最简单的例子,不同性别的人偏爱电影的类型会有所不同,如大部分男生可能比较...

    大数据综合实验,基于mapreduce实现的成绩分析系统,引入hadoop作云存储+源代码+文档说明

    1、资源内容:大数据综合实验,基于mapreduce的成绩分析系统,引入hadoop作云存储+源代码+文档说明 2、代码特点:内含运行结果,不会运行可私信,参数化编程、参数可方便更改、代码编程思路清晰、注释明细,都经过...

    【推荐】最强大数据学习与最佳实践资料合集(基础+架构+数仓+治理+案例)(100份).zip

    Hadoop精选面试题和MapReduce编程企业级编程实践 HDFS-part2-原理详解 HDFS核心内容及命令-2020 hive安装 hive语法和常用函数 Kafka安装 kylin安装 mapreduce调优指南 sqoop安装 二、架构篇 Flink-1.11 Hive集成与...

    Hadoop实战中文版

    3.3 读和写 3.3.1 InputFormat 3.3.2 OutputFormat 3.4 小结第二部分 实战 第4章 编写MapReduce基础程序 4.1 获得专利数据集 4.1.1 专利引用数据 4.1.2 专利描述数据 4.2 构建MapReduce 程序的基础模板 4.3...

    Python免费资源集合AwesomePython.zip

    Awesome Python环境管理开发包和依赖分发构建工具文件操作日期和时间文本处理自然语言处理文档配置命令行工具图像处理音频视频地理位置HTTP数据库数据库驱动ORMWeb框架CMSRESTful API身份认证模板引擎事件和任务队列...

    Hadoop实战(陆嘉恒)译

    实战第4 章 编写MapReduce基础程序4.1 获得专利数据集4.1.1 专利引用数据4.1.2 专利描述数据4.2 构建MapReduce 程序的基础模板4.3 计数4.4 适应Hadoop API 的改变4.5 Hadoop 的Streaming4.5.1 通过Unix命令使用...

    Hadoop实战

    433.3.1 InputFormat 443.3.2 OutputFormat 493.4 小结 50第二部分 实战第4章 编写MapReduce基础程序 524.1 获得专利数据集 524.1.1 专利引用数据 534.1.2 专利描述数据 544.2 构建MapReduce程序的基础模板 ...

    云计算第二版

    6.8 MapReduce编程 223 6.8.1 矩阵相乘算法设计 223 6.8.2 编程实现 224 习题 226 参考文献 226 第7章 Eucalyptus:Amazon云计算的开源实现 228 7.1 Eucalyptus简介 228 7.2 Eucalyptus技术实现 229 7.2.1 体系结构 ...

    Hadoop实战中文版.PDF

    524.1.1 专利引用数据 534.1.2 专利描述数据 544.2 构建MapReduce程序的基础模板 554.3 计数 604.4 适应Hadoop API的改变 644.5 Hadoop的Streaming 674.5.1 通过Unix命令使用Streaming 684.5.2 通过...

    FusionInsightHD华为大数据平台.pdf

    MapReduce是⼀种简化并⾏计算的编程模型,名字源于 该模型中的两项核⼼操作:Map和Reduce。Map将⼀个作业分解成为多个任务,Reduce将分解后多个任务处理的结果汇总起来,得 出最终的分析结果。 如图所⽰,MapReduce...

    什么是MEAN?JavaScript编程中的MEAN是什么意思?

    其实MENA就是 MongoDB(非关系数据库) + Express(模板引擎) + AngularJS(MVC javascript库) + NodeJS(服务器脚本)的简称。 它们共同构造 基于 javascript 的 现代web应用全栈开发工具。 MongoDB: 是一种强大...

    Python示例-从基础到高手PDF

    第 8 章 python 调用 zabbix 的 api 接口添加主机、查询组、主机、模板 第 9 章 python+Django 实现 Nagios 自动化添加监控项目 第 10 章 通过 python 和 websocket 构建实时通信系统[扩展 saltstack 监控] 第 11...

Global site tag (gtag.js) - Google Analytics