教你写map-reduce--lesson1

来源:互联网 发布:一加五 linux 编辑:程序博客网 时间:2024/06/05 04:32

MapReduce介绍-基础篇

一、基础概念

1.1         前言

MAP:映射

Reduce:聚合

       MapReduce是一种数据处理编程模型(google并行计算框架)

1.2         Mapreduce组件

描述

数量

JobTracker

Mapreduce任务管理器

1

TaskTracker

任务运行节点(计算节点)

N

 

 

 

 

 

 

 

1.3         Mapreduce概念

描述

数量

Jobclient

任务客户端,向JobTracker提交任务

 

Job

JobTracker接收提交的任务,生成一个job

 

Map

JobTracker将job分解成多个map任务

N

Reduce

JobTracker将job分解成多个reduce任务

N

 

 

二、计算模型

2.1           模型

每个Mapreduce任务都被初始化为一个job,job又可以分为两个阶段:Map阶段、reduce阶段。Map函数会接收一个<key,value>形式的输入,然后产生一个<key,value>输出,然后,hadoop会将具有相同key值的value集合<key,list<value>>,传递给reduce函数处理,reduce处理结束后,输出最终我们想要的结果,形式为<key,value>,输出会对key进行排序

 

List<Pair<key,value>> map(key,value)

List<Pair<key,value>>Reduce(key,list<value>)

2.2           一个简单的排序任务

对hdfs目录/wolf1下的文件进行排序

                   bin/hadoopjar contrib/streaming/hadoop-streaming-1.2.1.jar  -input /wolf1 -output /wolf2 -mapper /bin/cat-reducer /bin/cat

         两张oracle表进行join操作

Hive:用mr框架实现了一个类似sql的数据接口

Impala:cloudera

Select upper(a.name) from a outer left  joinb  on a.id = b.id

A的每一行作为map的输入,map输出<id,a的行>

B的每一行作为map的输入,map输出<id,b的行>

 

Reduce的输入<id, List<a的行, b的行>>

Mr是面向大数据的

Reduce1: map1 50G, map2 50G map3 50G

 

 

                  

三、Mr任务

3.1、Mr任务启动过程

        

 

 

步骤

描述

 

1、提交作业

1、通过调用JobTracker对象的getNewJobId方法从JobTracker获取job id

2、监察作业相关路径

3、将任务的划分信息写入split文件。Split信息主要包括:split文件头、split文件版本号、split个数

每一条又包括以下内容:split类型名(默认fileSplit)、split大小、split的内容(对于filesplit:文件名,文件中的位置),splitlocation信息(在哪个datanode上)

4、将运行所需要的资源上传hdfs:jar包、配置文件。

5、调用JobTracker对象的submitJob方法来真正提交作业

 

2、初始化作业

1、从hdfs中读取作业对应的job.split

2、创建并初始化Map和reduce任务

 

3、分配任务

将map、reduce任务分配到tasktracker上

 

4、执行任务

Tasktracker得到任务分配后

1、  将job.split复制到本地

2、  将job.jar赋值到本地

3、  将job的配置信息写入job.xml

4、  创建本地任务目录,解压job.jar

5、  调用launchTaskForJob方法发布任务

 

 

3.2           代码实例

 

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 {

StringTokenizeritr = 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 (IntWritableval : 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]));

 

job.waitForCompletion(true) ;

  }

}

 

四、交流

下一节任务:

1、  详细介绍编码,从oracle、hbase读取数据,进行mr处理,输出结果到hbase

调试、编码、性能优化

C++写MR

2、  介绍复杂的mr组合编码

 

0 0
原创粉丝点击