• protobuf 编译proto文件


    1. 下载protobuf

    protobuf的github:

    GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange format

    按照其md介绍所说,想要直接使用的话,可以使用pre-built版本的执行文件:

    点击链接进去,还是按照上边截图的说法找到对应平台的版本的zip包即可'protoc-$VERSION-$PALATFORM.zip',比如:

     

    下载解压即可,会有个exe执行文件:

     

    2. 编写protobuf

    接下来是编写protobuf,详细如何编写不介绍,感兴趣的同学去官方看一下,这里粘贴处java-vector-tile包的一个protobuf(其中的package名称修改为了'jffffff',用于做测试的):

    1. package jffffff;
    2. message Tile {
    3. // GeomType is described in section 4.3.4 of the specification
    4. enum GeomType {
    5. UNKNOWN = 0;
    6. POINT = 1;
    7. LINESTRING = 2;
    8. POLYGON = 3;
    9. }
    10. // Variant type encoding
    11. // The use of values is described in section 4.1 of the specification
    12. message Value {
    13. // Exactly one of these values must be present in a valid message
    14. optional string string_value = 1;
    15. optional float float_value = 2;
    16. optional double double_value = 3;
    17. optional int64 int_value = 4;
    18. optional uint64 uint_value = 5;
    19. optional sint64 sint_value = 6;
    20. optional bool bool_value = 7;
    21. extensions 8 to max;
    22. }
    23. // Features are described in section 4.2 of the specification
    24. message Feature {
    25. optional uint64 id = 1 [ default = 0 ];
    26. // Tags of this feature are encoded as repeated pairs of
    27. // integers.
    28. // A detailed description of tags is located in sections
    29. // 4.2 and 4.4 of the specification
    30. repeated uint32 tags = 2 [ packed = true ];
    31. // The type of geometry stored in this feature.
    32. optional GeomType type = 3 [ default = UNKNOWN ];
    33. // Contains a stream of commands and parameters (vertices).
    34. // A detailed description on geometry encoding is located in
    35. // section 4.3 of the specification.
    36. repeated uint32 geometry = 4 [ packed = true ];
    37. }
    38. // Layers are described in section 4.1 of the specification
    39. message Layer {
    40. // Any compliant implementation must first read the version
    41. // number encoded in this message and choose the correct
    42. // implementation for this version number before proceeding to
    43. // decode other parts of this message.
    44. required uint32 version = 15 [ default = 1 ];
    45. required string name = 1;
    46. // The actual features in this tile.
    47. repeated Feature features = 2;
    48. // Dictionary encoding for keys
    49. repeated string keys = 3;
    50. // Dictionary encoding for values
    51. repeated Value values = 4;
    52. // Although this is an "optional" field it is required by the specification.
    53. // See https://github.com/mapbox/vector-tile-spec/issues/47
    54. optional uint32 extent = 5 [ default = 4096 ];
    55. extensions 16 to max;
    56. }
    57. repeated Layer layers = 3;
    58. extensions 16 to 8191;
    59. }

    3. 执行命令生成java

    (我的protoc版本为:protoc-21.0-win64)

    接下来,进入到proto的目录,在当前位置开启cmd窗口,并新建'qwe'目录。

    输入命令($protoc_path --java_out=${your_java_package_dir} ${proto_file}):

    E:\protoc-21.0-win64\bin\protoc --java_out=qwe/ 1.proto

    生成结果:

    -tips:

    1. 这里编写一个输出java到绝对路径的方式(cmd在1.proto目录下):

    E:\protoc-21.0-win64\bin\protoc --java_out=G:\tes\eee 1.proto

    2. 再写一个proto位置为相对路径的(cmd在eee目录下):

    1. # 这两个都可以
    2. E:\protoc-21.0-win64\bin\protoc --java_out=G:\tes\eee eee/1.proto
    3. E:\protoc-21.0-win64\bin\protoc --java_out=G:\tes\eee eee\1.proto

    3. 这样报错:

    1. G:\tes>E:\protoc-21.0-win64\bin\protoc --java_out=G:\tes\eee --proto_path=G:\tes\eee\1.proto
    2. Missing input file.

    4. 

  • 相关阅读:
    【LeetCode每日一题】——面试题10.11.峰与谷
    leetcode做题笔记166. 分数到小数
    java基于ssm+vue高校人事管理系统
    C++基础——指针
    ASP.NET-实现图形验证码
    tomcat命令行下启动中文乱码(解释原理版)
    股票换手率排行查询易语言代码
    STL-list的使用及其模拟实现
    如何阻止事件冒泡和默认事件
    中断机制-通过AtomicBoolean实现线程中断停止
  • 原文地址:https://blog.csdn.net/jfqqqqq/article/details/126484962