• Feeds

  • Thrift, Protocol Buffers installation and Java code howto

    I. Thrift installation and Java code

    1. build and install thrift

    install boost
    cd <boost_root>/tools/jam
    ./build_dist.sh
    # linux* will depends the platform
    cp stage/bin.linux*/bjam <boost_root>
    # build boost, use bjam will faster
    cd <boost_root>
    ./configure –without-icu –prefix=/usr/local/boost
    ./bjam -toolset=gcc –build-type=release install –prefix=/usr/local/boost

    # build thrift
    ./bootstrap.sh
    ./configure –with-boost=/usr/local
    make
    make install

    2. Build Thrift java library

    install apache ant if necessary

    cd lib/java/
    ant

    get libthrift.jar

    3. Create .thrift file and gen Java code

    (See http://wiki.apache.org/thrift/Tutorial for more .thrift tutorial info)
    tim.thrift

    struct dns_record {
    1: string key,
    2: string value,
    3: string type = 'A',
    4: i32 ttl = 86400,
    5: string first,
    6: string last
    }
    
    service TestDns {
    dns_record test(1:string q);
    }

    <thrift_root>/bin/thrift –gen java tim.thrift
    code will be generated in gen-java/*.java

    4. Write java code

    // new object
    dns_record dr = new dns_record(key, value, type, ttl, first, last)
    // serialize
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    byte[] bytes = serializer.serialize(dr);

    see also: http://wiki.apache.org/thrift/ThriftUsageJava

    II. Protocol Buffers install and Java code

    1. Build and install Protocol buffers

    ./configure
    make
    make install

    2. Build protobuf Java library

    install maven if not necessary

    cd protobuf/java
    mvn test
    mvn package

    get jar from target/protobuf-java-x.x.x.jar

    3. Create .proto file and gen Java code

    tim.proto

    package dns;
    
    message DnsRecord {
    required string key = 1;
    required string value = 2;
    required string first = 3;
    required string last = 4;
    optional string type = 5 [default = "A"];
    optional int32  ttl = 6 [default = 86400];
    }
    
    message DnsResponse {
    repeated DnsRecord records = 1;
    }

    bin/protoc –java_out . tim.proto

    4. Write Java code

    // protocol buffer need a builder to create object
    Dns.DnsRecord.Builder b = Dns.DnsRecord.newBuilder();
    b.setKey("key");
    b.setValue("value...");
    ...
    b.builder();
    
    byte[] bytes = dr.toByteArray();
    Dns.DnsRecord dr2 = Dns.DnsRecord.parseFrom(bytes);

    III. Resources

    Thrift: http://incubator.apache.org/thrift/

    Protocol Buffers: http://code.google.com/apis/protocolbuffers/

    Tim’s Blog: http://timyang.net/

    如想及时阅读Tim Yang的文章,可通过页面右上方扫码订阅最新更新。

    « | »

    3 Comments  »

    1. Michael Luo

      This is the perf testing vs thrift-0.30.0(with thrift_win32_compiler v0.30rc4) vs protobuf 2.3.0. By default, the protobuf is quite fast and compact in size, maybe the speed option is enabled by default?

      This is the thrift, v0.30rc4
      Thrift Loop : 1,000,000
      Get object : 938msec
      Serdes thrift : 6,578msec
      Objs per second: 152,021
      Total bytes : 113,000,000

      This is the default protobuf without option optimize_for = SPEED;
      ProtoBuf Loop : 1,000,000
      Get object : 546msec
      Serdes protobuf: 4,922msec
      Objs per second: 203,169
      Total bytes : 82,997,522

      This is the default protobuf with option optimize_for = SPEED;
      ProtoBuf Loop : 1,000,000
      Get object : 547msec
      Serdes protobuf: 4,891msec
      Objs per second: 204,457
      Total bytes : 83,000,886

    Leave a Comment