Yarn核心——Protobuf

本文介绍YARN如何利用Protobuf进行RPC通信,并通过示例详细展示了Protobuf的定义、编译及序列化过程。从student.proto文件的定义到序列化后的数据传输,完整地演示了整个流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: 1
name: "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: 1
name: "jack"
sex: "2"
phones {
  number: "12345"
  type: 1
}
phones {
  number: "67890"
  type: 2
}

ID: 1
name: "jack"
sex: "2"
phones {
  number: "12345"
  type: 1
}
phones {
  number: "67890"
  type: 2
}

Yarn中通信都是通过protobuf定义的,默认实现protobuf。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值