【深入浅出 Yarn 架构与实现】2-1 Yarn 基础库概述
了解 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 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.核心特点:
- 语言、平台无关
- 简洁
- 高性能
- 兼容性好
【【深入浅出 Yarn 架构与实现】2-1 Yarn 基础库概述】
# 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 --versionlibprotoc 3.19.1
3、写个 demo1)创建个 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("zhangsan@yahoo.com").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!");}}}
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 吸引人的水果店名 水果店取什么名字最旺
- 4大星座在开学后 学业运高涨
- 2023年4月宜订婚的日子是哪天
- 2023年4月什么日子做生意最好
- 征信会影响公积金贷款额度吗
- 楼板下无梁可以砌墙吗
- 提前还贷银行有什么要求
- 2023年12月哪天相亲最好最吉利
- 2023年五行缺火的男孩适合取的名字100个
- 2023年有火有土的男孩名字有哪些