Yarn核心——Protobuf
来源:互联网 发布:知识付费行业数据 编辑:程序博客网 时间:2024/06/03 16:31
Yarn将Protobuf用在RPC通信中,基于Protobuf的方式进行通信。Yarn RPC中的所有参数都采用Protocol Buffer进行序列化和反序列化,体积小,速度快。
(1)Proto文件 student.proto
package tutorial;option java_package="com.jackniu.hadoop_yarn.com.proto";option java_outer_classname="StudentProtos";message Student{ required int32 ID=1; required string name=2; required string sex=3; message StudentPhone{ required string number=1; optional int32 type=2; } repeated StudentPhone phones=4;}
(2) 编译
bin/protoc --java_out=javacode code/student.proto
(3) 编译完成部分代码
package com.jackniu.hadoop_yarn.com.proto;public final class StudentProtos { private StudentProtos() {} public static void registerAllExtensions( com.google.protobuf.ExtensionRegistry registry) { } public interface StudentOrBuilder extends com.google.protobuf.MessageOrBuilder { // required int32 ID = 1; /** * <code>required int32 ID = 1;</code> */ boolean hasID(); /** * <code>required int32 ID = 1;</code> */ int getID();
(4) 使用
package com.jackniu.hadoop_yarn.com.proto;/** * Created by JackNiu on 2017/9/1. */public class ProtocolBufferExample { public static void main(String[] args) { StudentProtos.Student student = StudentProtos.Student.newBuilder().setName("jack").setID(1).setSex("2") .addPhones(StudentProtos.Student.StudentPhone.newBuilder().setNumber("12345").setType(1)) .addPhones(StudentProtos.Student.StudentPhone.newBuilder().setNumber("67890").setType(2)) .build(); System.out.println(student); // 在发送端发送 }}ID: 1name: "jack"sex: "2"phones { number: "12345" type: 1}phones { number: "67890" type: 2}
(5) 发送和读取
package com.jackniu.hadoop_yarn.com.proto;import org.apache.hadoop.mapred.FileOutputFormat;import java.io.FileInputStream;import java.io.FileOutputStream;/** * Created by JackNiu on 2017/9/1. */public class ProtocolBufferExample { public static void main(String[] args) throws Exception{ StudentProtos.Student student = StudentProtos.Student.newBuilder().setName("jack").setID(1).setSex("2") .addPhones(StudentProtos.Student.StudentPhone.newBuilder().setNumber("12345").setType(1)) .addPhones(StudentProtos.Student.StudentPhone.newBuilder().setNumber("67890").setType(2)) .build(); System.out.println(student); //在发送端发送 FileOutputStream output = new FileOutputStream("example.txt"); student.writeTo(output); output.close(); FileInputStream inputStream = new FileInputStream("example.txt"); StudentProtos.Student receive_student = StudentProtos.Student.parseFrom(inputStream); System.out.println(receive_student); }}输出: example.txt jack 2" 12345 " 67890控制台输出ID: 1name: "jack"sex: "2"phones { number: "12345" type: 1}phones { number: "67890" type: 2}ID: 1name: "jack"sex: "2"phones { number: "12345" type: 1}phones { number: "67890" type: 2}
Yarn中通信都是通过protobuf定义的,默认实现protobuf。
阅读全文
0 0
- Yarn核心——Protobuf
- Yarn核心——RPC(3) YarnRPC
- Yarn核心——RPC(1) 动态代理
- Yarn核心——RPC(2) Hadoop RPC
- YARN核心部件解读
- yarn的核心设计介绍
- Hadoop核心组件之Yarn
- Hadoop核心模块之Yarn
- 69.Protobuf进阶——使用Python操作Protobuf
- Hadoop——Hadoop Yarn
- Spark配置—Yarn模式
- caffe源码分析(1)——protobuf
- Unity3D —— protobuf网络框架
- Unity3D —— protobuf网络框架
- yarn —— Nodejs包新管理工具
- Hadoop的资源管理——Yarn初探
- YARN编程实例—distributedshell源码分析
- YARN/MRv2 MRAppMaster深入剖析—概述
- Hybrid APP混合开发的了解和其他两类app的对比
- MySQL入门很简单-索引
- Yarn基本命令行
- Android8.0新特性和API
- 网络编程函数使用
- Yarn核心——Protobuf
- CentOS7下Tomcat9的安装
- DVRRDK的系统开发
- Web测试
- C++软件开发经典面试题目
- 米语录学习(一)
- 设计模式汇总
- 决策树实战项目-鸢尾花分类
- appium升级1.6 + iOS10 +Xcode8