protobuf编译.proto文件到.java_ccllcaochong1的博客-程序员宅基地

技术标签: protobuf  

一。下载protoc.exe工具

http://central.maven.org/maven2/com/google/protobuf/protoc/3.5.1/protoc-3.5.1-windows-x86_64.exe 

二。准备.proto文件

如:CubeVisit.proto

syntax = "proto2";
option java_package = "org.apache.kylin.storage.hbase.cube.v2.coprocessor.endpoint.generated";

option java_outer_classname = "CubeVisitProtos1";

option java_generic_services = true;

option java_generate_equals_and_hash = true;

option optimize_for = SPEED;

message CubeVisitRequest {
    required bytes gtScanRequest = 1;
    required bytes hbaseRawScan = 2;
    required int32 rowkeyPreambleSize = 3;
    repeated IntList hbaseColumnsToGT = 4;
    required string kylinProperties = 5; // kylin properties
    optional string queryId = 6;
    optional bool spillEnabled = 7 [default = true];
    optional int64 maxScanBytes = 8; // must be positive
    optional bool isExactAggregate = 9 [default = false];
    message IntList {
        repeated int32 ints = 1;
    }
}

message CubeVisitResponse {
    message Stats {
        optional int64 serviceStartTime = 1;
        optional int64 serviceEndTime = 2;
        optional int64 scannedRowCount = 3;
        optional int64 aggregatedRowCount = 4;
        optional double systemCpuLoad = 5;
        optional double freePhysicalMemorySize = 6;
        optional double freeSwapSpaceSize = 7;
        optional string hostname = 8;
        optional string etcMsg = 9;
        optional int32 normalComplete =10;
        optional int64 scannedBytes = 11;
        optional int64 filteredRowCount = 12;
    }
    enum ErrorType {
        UNKNOWN_TYPE = 0;
        TIMEOUT = 1;
        RESOURCE_LIMIT_EXCEEDED = 2;
    }
    message ErrorInfo {
        required ErrorType type = 1;
        required string message = 2;
    }
    required bytes compressedRows = 1;
    required Stats stats = 2;
    optional ErrorInfo errorInfo = 3; // should be set when stats.normalComplete == false
}

service CubeVisitService {
    rpc visitCube (CubeVisitRequest) returns (CubeVisitResponse);
}

三。编译

示例:D:>protoc-3.5.1-windows-x86_64.exe --proto_path F:\ideaproject\kylin-kylin-2.6.0\storage-hbase\src\main\java\org\apache\kylin\storage\hbase\cube\v2\coprocessor\endpoint\protobuf --java_out ./ CubeVisit1.proto

  • protoc,编译命令;
  • --proto_path,就是你的proto文件所在目录是哪。我这里是F:\ideaproject\kylin-kylin-2.6.0\storage-hbase\src\main\java\org\apache\kylin\storage\hbase\cube\v2\coprocessor\endpoint\protobuf。
  • --java_out,标识输出的java文件应该放在哪个目录。这里的 ./ 是指当前目录。
  • protoDemo.proto,就是我们要编译的文件

注意:生成的RPC service的类CubeVisitService是在CubeVisitProtos1里面的,它是一个抽象类,你要使用的话需要继承并实现它的方法。

四。java使用

<dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.6.0</version>
        </dependency>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ccllcaochong1/article/details/98892876

智能推荐

随便推点