• ProtoBuffer


    序列化数据的要求

    1.        效率  时间空间
    2.        多语言相互操作
    3.        使用方便

    ProtoBuffer 使用:

    1. Designing objects

      Person:
      Id
      Name
      Age
      Email
      Phone(s)
      

        

    2. Describing objects
      Person:
      required int32 id
      required string name
      optional string email
      repeated string phone
    3. Compiling the description
      package tutorial;
      option java_package  = "com.example.tutorial";
      option java_outer_classname  = "AddressBookProtos";
      message Person {
      required string name = 1;
      required int32 id = 2;
      optional  string email = 3;
      enum PhoneType {
      MOBILE = 0;
      HOME = 1;
      WORK = 2;
      }
      message PhoneNumber {
      required string number = 1;
      optional  PhoneType type = 2 [default = HOME];
      }
      repeated PhoneNumber  phone = 4;
      }
      message  AddressBook {
      repeated Person person  = 1;
      }
      protoc --java_out=$DST_DIR addressbook.proto
    4. Obtaining the generated source-code
    5. Importing objects into your project

    6. Instantiating objects
      Person  john = Person.newBuilder()
      .setId(12345)
      .setName
      .setEmail
      .addPhone(Person.PhoneNumber.newBuilder()
      .setNumber("+351 999 999 999")
      .setType(Person.PhoneType.HOME)
      .build())
      .build();
    7. Using objects
      // Writing data to a file
      FileOutputStream  aOutput  = new FileOutputStream("theFilename")
      Person  aPerson = Person.newBuilder.set()....   //instance  a Person
      aPerson.writeTo(aOutput);
      aOutput.close();
      // Reading data from a file Person aPerson = Person.parseFrom(new FileInputStream("theFilename")) // Do something with the received Person

    ProtoBuffer 重点在

    1. Efficiency (space and time)   效率 (空间和时间)

    2. Language interoperability     多语言互操作性
    3. Usability                             方便使用

    其它解决方案

    1. Avro (http://avro.apache.org/)

    2. Thrift (http://thrift.apache.org/)
    3. Kryo (https://github.com/EsotericSoftware/kryo)
  • 相关阅读:
    mybatis设置select返回HashMap,字段值为null时,不会保存key
    copyProperties 忽略null值字段
    动态新增表单
    页面时时刷新数据
    FormData 传List对象集合到后台
    linux常用命令总结
    Linux部署安装JDK
    yum安装nginx
    01_Python基础
    Nginx防盗链
  • 原文地址:https://www.cnblogs.com/empireghost/p/3939615.html
Copyright © 2020-2023  润新知