关于hadoop:深入浅出-Yarn-架构与实现21-Yarn-基础库概述

4次阅读

共计 3651 个字符,预计需要花费 10 分钟才能阅读完成。

理解 Yarn 根底库是前面浏览 Yarn 源码的根底,本节对 Yarn 根底库做总体的介绍。
并对其中应用的第三方库 Protocol Buffers 和 Avro 是什么、怎么用做简要的介绍。

一、次要应用的库

  • Protocol Buffers:是 Google 开源的序列化库,具备平台无关、高性能、兼容性好等长处。YARN 将其用到了 RPC 通信中,默认状况 下,YARN RPC 中所有参数采纳 Protocol Buffers 进行序列化 / 反序列化。
  • Apache Avro:是 Hadoop 生态系统中的 RPC 框架,具备平台无关、反对动静模式 (无需编译) 等长处,Avro 的最后设计动机是解决 YARN RPC 兼容性和扩展性 差等问题。
  • RPC 库:YARN 仍采纳了 MRv1 中的 RPC 库,但其中采纳的默认序列化办法被替换成了 Protocol Buffers。
  • 服务库和事件库 :YARN 将所有的对象服务化,以便对立治理(比创立、销毁等),而服务之间则采纳事件机制进行通信,不再应用相似 MRv1 中基于函数调用的形式。
  • 状态机库:YARN 采纳无限状态机形容一些对象的状态以及状态之间的转移。引入状态机模型后,相比 MRv1,YARN 的代码构造更加清晰易懂。

    二、第三方开源库介绍

    一)Protocol Buffers

    1、简要介绍

    Protocol Buffers 是 Google 开源的一个语言无关、平台无关的通信协议,其玲珑、高效和敌对的兼容性设计,使其被宽泛应用。
    【能够类比 java 自带的 Serializable 库,性能上是一样的。】

    Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

外围特点:

  • 语言、平台无关
  • 简洁
  • 高性能
  • 兼容性好

    2、装置环境

    以 mac 为例(其余平台形式请自查)

    # 1) brew 装置
    brew install protobuf 
    
    # 查看装置目录
    $ which protoc 
    /opt/homebrew/bin/protoc 
    
    
    # 2) 配置环境变量
    vim ~/.zshrc
    
    # protoc (for hadoop)
    export PROTOC="/opt/homebrew/bin/protoc"
    
    source ~/.zshrc
    
    
    # 3) 查看 protobuf 版本
    $ protoc --version
    libprotoc 3.19.1

    3、写个 demo

    1)创立个 maven 工程,增加依赖

    <dependencies>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.19.1</version>  <!-- 版本号务必和装置的 protoc 版本统一 -->
    </dependency>
    </dependencies>

    2)根目录新建 protobuf 的音讯定义文件 student.proto

    proto 数据类型语法定义能够参考:ProtoBuf 入门教程

syntax = "proto3"; // 申明为 protobuf 3 定义文件
package tutorial;

option java_package = "com.shuofxz.learning.student";    // 生成文件的包名
option java_outer_classname = "StudentProtos";                // 类名

message Student {                                // 待形容的结构化数据
    string name = 1;
    int32 id = 2;
    optional string email = 3;    //optional 示意该字段能够为空

    message PhoneNumber {                // 嵌套构造
        string number = 1;
        optional int32 type = 2;
    }

    repeated PhoneNumber phone = 4;    // 反复字段
}

3)应用 protoc 工具生成音讯对应的 Java 类(在 proto 文件目录执行)

protoc -I=. --java_out=src/main/java student.proto

能够在对应的文件夹下找到 StudentProtos.java 类,外面写了序列化、反序列化等办法。

public class StudentExample {static public void main(String[] argv) {StudentProtos.Student Student1 = StudentProtos.Student.newBuilder()
                .setName("San Zhang")
                .setEmail("[email protected]")
                .setId(11111)
                .addPhone(StudentProtos.Student.PhoneNumber.newBuilder()
                        .setNumber("13911231231")
                        .setType(0))
                .addPhone(StudentProtos.Student.PhoneNumber.newBuilder()
                        .setNumber("01082345678")
                        .setType(1)).build();

        // 写出到文件
        try {FileOutputStream output = new FileOutputStream("example.txt");
            Student1.writeTo(output);
            output.close();} catch(Exception e) {System.out.println("Write Error !");
        }

        // 从文件读取
        try {FileInputStream input = new FileInputStream("example.txt");
            StudentProtos.Student Student2 = StudentProtos.Student.parseFrom(input);
            System.out.println("Student2:" + Student2);
        } catch(Exception e) {System.out.println("Read Error!");
        }
    }
}

以上就是一个 protocol buffers 应用的残缺流程了。没什么难的,就是调用了一个第三方的序列化库,将对象序列化到文件,再反序列化读出来。
只不过须要先在 proto 文件中定义好数据结构,并生成对应的工具类。

4、在 Yarn 中利用

在 YARN 中,所有 RPC 函数的参数均采纳 Protocol Buffers 定义的。RPC 仍应用 MRv1 中的 RPC。

二)Apache Avro

1、简要介绍

Apache Avro 是 Hadoop 下的一个子项目。它自身既是一个序列化框架,同时也实现 了 RPC 的性能。
但因为 Yarn 我的项目初期,Avro 还不成熟,Avro 则作为日志序列化库应用,所有事件的序列化均采纳 Avro 实现。
特点:

  • 丰盛的数据结构类型;
  • 疾速可压缩的二进制数据模式;
  • 存储持久数据的文件容器;
  • 提供近程过程调用 RPC;
  • 简略的动静语言联合性能。

相比于 Apache Thrift 和 Google 的 Protocol Buffers,Apache Avro 具备以下特点:

  • 反对动静模式。Avro 不须要生成代码,这有利于搭建通用的数据处理系统,同时防止了代码入侵。
  • 数据毋庸加标签。读取数据前,Avro 可能获取模式定义,这使得 Avro 在数据编码时只须要保留更少的类型信息,有利于缩小序列化后的数据大小。
  • 毋庸手工调配的域标识。Thrift 和 Protocol Buffers 应用一个用户增加的整型域唯一性定义一个字段,而 Avro 则间接应用域名,该办法更加直观、更加易扩大。

    2、装置环境 & demo

    参考:Avro 学习入门

3、在 Yarn 中利用

Apache Avro 最后是为 Hadoop 量身打造的 RPC 框架,思考到稳定性,YARN 临时采纳 Protocol Buffers 作为序列化库,RPC 仍应用 MRv1 中的 RPC,而 Avro 则作为日志序列化库应用。在 YARN MapReduce 中,所有事件的序列化 / 反序列化均采纳 Avro 实现,相干定义在 Events.avpr 文件中。

三、总结

本节简要介绍了 Yarn 中五个重要的根底库,理解这些库会帮忙理解 Yarn 代码逻辑和数据传递形式。
对其中两个第三方开源库进行了介绍。Protocol Buffers 用作 RPC 函数参数的序列化和反序列化;Avro 在日志和事件局部的序列化库应用。

正文完
 0