简体   繁体   English

Java和Google协议缓冲区:有没有人有一个简单的入门例子?

[英]Java & Google Protocol Buffers: Does anyone have a simple example of getting started with this?

I'm not really sure where to start. 我不确定从哪里开始。

Like, some sites I saw wanted me to install maven, and do a bunch of things with copying files to system directories, and adding those directories to $path (which I hate doing, btw, the devs should just write an installer). 就像,我看到的一些网站要我安装maven,并做一些事情,将文件复制到系统目录,并将这些目录添加到$ path(我讨厌做,顺便说一下,开发人员应该只写一个安装程序)。 But anyway, the library needs to be packaged with my app, so I'm not interested in installing Protocol Buffers... Just attacking it to my java project in eclipse. 但无论如何,库需要与我的应用程序打包在一起,所以我对安装Protocol Buffers不感兴趣...只是在eclipse中攻击它到我的java项目。 (like, adding jars to the build path.) (比如,将jar添加到构建路径中。)

Any tutorials you guys know of? 你们知道的任何教程?

You don't need to do any installation on production boxes. 您无需在生产箱上进行任何安装。 You need to install it on the build box so that you can create java bindings for the .proto file you write. 您需要在构建框上安装它,以便您可以为您编写的.proto文件创建Java绑定。 This is similar to the way you generate code from wsdl or xsd. 这类似于从wsdl或xsd生成代码的方式。 Once you the the generated code (that helps in serialize/deserialize of the binary message), you can bundle it as a jar and use it like any other jar library. 一旦生成了代码(有助于序列化/反序列化二进制消息),就可以将其捆绑为jar并像使用任何其他jar库一样使用它。 Here is a small tutorial to get you started. 这是一个小型教程 ,可以帮助您入门。

Just to add more clarity 只是为了增加清晰度

Protobuf   = XML Schema
.proto     = xsd
protoc.exe = xjc

The process is as follows 该过程如下

  1. Build a .proto file that provided meta info 构建提供元信息的.proto文件
  2. run the .proto through the protoc.exe for it to generate code 通过protoc.exe运行.proto以生成代码
  3. Bundle the generated code into a jar file (or just import all that code to your source tree) 将生成的代码捆绑到一个jar文件中(或者只是将所有代码导入到源代码树中)
  4. Add the jar file to build path 添加jar文件以构建路径
  5. Use the generated code in your application for ser/deser 使用应用程序中生成的代码进行ser / deser

Using maven greatly simplifies 2, 3 and 4 使用maven大大简化了2,3和4

Hope this would help. 希望这会有所帮助。

Describes how to convert ProtoBuf schema into java File http://hecktechsolutions.blogspot.in/2015/01/protocol-buffer.html 描述如何将ProtoBuf模式转换为java文件http://hecktechsolutions.blogspot.in/2015/01/protocol-buffer.html

Also gives few words on Eclipse Setup at End 在Eclipse的Eclipse Setup中也提供了一些文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM