大数据之MapReduce

MapReduce简介

MapReduce的地位

HDFS:分布式存储系统
MapReduce:分布式计算系统
YARN: Hadoop 的资源调度系统
Common: 以上三大组件的底层支撑组件,主要提供基础工具包和 RPC 框架等

MapReduce的定义

(1)MapReduce是一个基于集群的高性能并行计算平台。它允许用普通的商用服务器构成一个包含数十、数百至数千个节点的分布和并行计算集群
(2)MapReduce是一个并行计算与运行软件框架。它提供了一个庞大但设计精良的并行计算软件框架,能自动完成的计算任务的并行化处理,自动划分计算数据和计算任务,在集群节点上自动分配和执行任务以及收集计算结果,将数据分布存储、数据通信、容器处理等并行计算涉及到的很多系统底层的负责细节交由系统负责处理
(3)MapReduce是一个并行成勋涉及模型与方法。它借助于函数式编程语言Lisp的涉及思想,提供了一种简单的并行程序设计方法,用Map和Reduce两个函数编程实现基本的并行计算任务,提供了抽象的操作和并行编程接口,以简单方便地完成大规模数据的编程和计算处理

Map和Reduce的工作原理

MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算。MapReduce将用用划分未Map和Reduce两个步骤,其中Map对数据集上的独立元素进行指定的操作,生成键值对形式的中间结果。Reduce则对中间结果中相同"键"的所有"值"进行规约,以得到最终结果。MapReduce

MapReduce的Demo

MapReduce基础知识

Cbiner函数
本地化的reducer
Partitioner函数
决定着Map节点的输出将被分区到哪个Reduce节点
什么是shuffle
怎样把map task的输出结果有效地传送到reduce端?
map输出之前,在内存里经过sort和combiner,再将所有的输出集合到partitioner进行划分到不同的reducer,在每个分区(partition)中,再进行内存中排序,再运行combiner,最后输出到HDFS

准备工作

新建一个Maven项目
在这里插入图片描述

通过如下网站:
https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common/2.7.3

<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.7.3</version>
</dependency>

将上述代码复制到Maven中的pom文件中,如下入所示

在这里插入图片描述

创建Java文件
在这里插入图片描述

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.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;

import java.io.IOException;
import java.util.StringTokenizer;

public class wordCount {

	public static class TokenizerMapper 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.hasMoreTokens()) {
				word.set(itr.nextToken());
				context.write(word, one);
			}
		}
	}

	public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = 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();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}
		Job job = new Job(conf, "word count");
		job.setJarByClass(wordCount.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(IntSumReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}

使用上述代码可构建MapReduce程序,在hadoop下运行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值