zoukankan      html  css  js  c++  java
  • MapReduce概述

    1.1 MapReduce定义

    MapReduce是一个分布式运算程序的编程框架,是用户开发“基于Hadoop的数据分析应用”的核心框架。
    MapReduce核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的分布式运算程序,并发运行在一个Hadoop集群上。

    1.2 MapReduce优缺点

    1.2.1 优点

    • 易于编程:用户只关心业务逻辑,实现框架的接口。
    • 良好的扩展性:可以动态增加服务器,解决计算资源不够的问题。
    • 高容错性: 任何一台机器宕机,可以将任务转移到其它节点。
    • 适合海量数据的计算:TB、PB级别,几千台服务器共同计算。

    1.2.2 缺点

    • 不擅长实时计算:无法达到mysql的毫秒级计算
    • 不擅长流式计算
    • 不擅长DAG有向无环图计算

    1.3 MapReduce核心编程思想

    1. MapReduce运算程序一般需要分成2个阶段:Map阶段和Reduce阶段
    2. Map阶段完全并发运行,互不相干
    3. Reduce阶段的并发ReduceTask,完全互不相干,但是他们的数据依赖于上一个阶段的所有MapTask并发实例的输出
    4. MapReduce编程模型智能包含一个Map阶段和一个Reduce阶段,如果用户的业务逻辑非常复杂,那就智能多个MapReduce程序,串行运行

    1.4 MapReduce进程

    一个完整的 MapReduce 程序在分布式运行时有三类实例进程:
    (1)MrAppMaster:负责整个程序的过程调度及状态协调。
    (2)MapTask:负责 Map 阶段的整个数据处理流程。
    (3)ReduceTask:负责 Reduce 阶段的整个数据处理流程。

    1.5 常用数据序列化类型

    Java类型 Hadoop Writable类型
    Boolean BooleanWritable
    Byte ByteWritable
    Int IntWritable
    Float FloatWritable
    Long LongWritable
    Double DoubleWritable
    String Text
    Map MapWritable
    Array ArrayWritable
    Null NullWritable

    1.6 MapReduce编程规范

    用户编写的程序分成三个部分:Mapper、Reducer、Driver
    1.Mapper阶段

    • 用户自定义的Mapper要继承自己的父类
    • Mapper的输入数据是KV对的形式
      key是这一行开头的偏移量,value是对应这一行的内容
    • Mapper中的业务逻辑写在map()方法中
    • Mapper的输出数据是KV对的形式
    • map()方法(MapTask进程)对每一个<K, V>调用一次

    2.Reducer阶段

    • 用户自定义的Reducer要继承自己的父类
    • Reducer的输入数据类型对应Mapper的输出数据类型,也是KV
    • Reducer的业务逻辑写在reduce()方法中
    • ReduceTask进程对每一组相同的k的<K, V>组调用一次reduce()方法

    3.Driver阶段
    相当于YARN集群的客户端,用于提交我们整个程序到YARN集群,提交的是封装了MapReduce程序相关运行参数的job对象

    1.7 WordCount案例编写

    1.7.1 准备数据文件

    1.7.2 需求分析

    Mapper:

    1. 将MapTask传给我们的文本内容先转换成string(因为string的api比较多)
    2. 根据分割符将这一行切分成单词
    3. 将单词输出为<单词, 1>

    Reduce:

    1. 汇总各个key的个数
    2. 输出key的总次数

    Driver:

    1. 获取配置信息,获取job对象实例
    2. 指定本程序的jar包所在的本地路径
    3. 关联Mapper和Reducer业务类
    4. 指定Mapper输出数据的KV类型
    5. 指定最终输出数据的KV类型
    6. 指定job的输入原始文件的所在目录
    7. 指定job的输出结果所在目录(输出结果路径不能提前存在)
    8. 提交作业

    1.7.3 环境准备

    (1)创建 maven 工程,MapReduceDemo
    (2)在 pom.xml 文件中添加如下依赖

    <dependencies>
      <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
       <version>2.10.0</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.30</version>
      </dependency>
    </dependencies>
    

    (3)在项目的 src/main/resources 目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。

    log4j.rootLogger=INFO, stdout 
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n 
    log4j.appender.logfile=org.apache.log4j.FileAppender 
    log4j.appender.logfile.File=target/spring.log 
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 
    log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
    

    (4)编写程序
    Mapper

    public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    
        private Text outKey = new Text();
        private IntWritable outValue = new IntWritable(1);
    
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 1.将MapTask传给我们的文本内容先转换成string
            String line = value.toString();
    
            // 2.根据分割符将这一行切分成单词
            String[] words = line.split(" ");
    
            // 3.循环写出
            for (String word : words) {
                outKey.set(word);
                context.write(outKey, outValue);
            }
        }
    }
    

    Reducer

    public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    
        private IntWritable outValue = new IntWritable();
    
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            outValue.set(sum);
            context.write(key, outValue);
        }
    }
    

    Driver

    public class WordCountDriver {
    
        public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
            Configuration conf = new Configuration();
            // 1.获取job
            Job job = Job.getInstance(conf);
    
            // 2. 设置jar包路径
            job.setJarByClass(WordCountDriver.class);
    
            // 3. 关联mapper reducer
            job.setMapperClass(WordCountMapper.class);
            job.setReducerClass(WordCountReducer.class);
    
            // 4. 设置map输出的kv类型
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
    
            // 5. 设置最终输出的kv类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
    
            // 6. 设置输入路径和输出路径
            FileInputFormat.setInputPaths(job, new Path(args[0]));
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
    
            // 7. 提交job
            boolean b = job.waitForCompletion(true);
    
            System.exit(b ? 0 : 1);
    
        }
    }
    

    (5)将jar文件上传到Linux
    (6)运行

    hadoop jar /opt/jars/wordcount.jar com.gazikel.mapreduce.wordcount.WordCountDriver /mapreduce_study/in/hello.txt /mapreduce_study/out/wordcount
    

  • 相关阅读:
    AngularJS之ng-repeat指令
    AngularJS之ng-controller指令
    HttpClient通过Post上传文件
    response.setHeader()的用法
    response 设置响应头的常用几种方法
    理解HTTP session原理及应用
    mysqldump 利用rr隔离实现一致性备份
    Struts标签、Ognl表达式、el表达式、jstl标签库这四者之间的关系和各自作用
    AngularJS之ng-model指令
    AngularJS之ng-app指令
  • 原文地址:https://www.cnblogs.com/Gazikel/p/15574699.html
Copyright © 2011-2022 走看看