Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.
You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
特性解读
structured data
如果在一个大项目中,使用了多种语言,而且他们之间还涉及通信,这时 protobuf
将成为一个利器,
只需定义一次protobuf规范的Message结构,然后生成不同语言的Message类。 所以这里的关键就是抽象出Message.
序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程。(百度百科)
In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment. (维基百科)
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization. (MSDN)
从定义可以看出,序列化的关键有
这个实例演示了,如何通过Python程序向address_book文件写数据,通过C++程序从address_book文件读取数据,并且还原为原来的结构。
root@ubuntu:~# pwd
/root
root@ubuntu:~# tree protobuf/
protobuf/
├── addressbook.proto
├── reader.cc
└── writer.py
0 directories, 3 files
编译proto文件,生成相应的Python文件,它包含相关类定义等
编译命令: protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto
root@ubuntu:~# SRC_DIR=protobuf/
root@ubuntu:~# DST_DIR=protobuf/
root@ubuntu:~# protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto
在Python程序中导入protoc生成的模块,直接执行。
root@ubuntu:~# python protobuf/writer.py
address_book : person {
name: "Dave s0"
id: 0
email: ""
phone {
number: "13233333333"
type: MOBILE
}
}
....
并将person信息保存到当前目录的address_book文件中。
root@ubuntu:~# file address_book
address_book: data
编译命令: protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
root@ubuntu:~# SRC_DIR=protobuf/
root@ubuntu:~# DST_DIR=protobuf/
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
编译C++程序时需要加上链接选项-lprotobuf
root@ubuntu:~# g++ $SRC_DIR/reader.cc $SRC_DIR/addressbook.pb.cc -lprotobuf -o $DST_DIR/reader
root@ubuntu:~# $DST_DIR/reader address_book
Person ID: 0
Name: Dave s0
E-mail address:
Mobile phone #: 13233333333
...
root@ubuntu:~# tree protobuf/
protobuf/
├── addressbook_pb2.py
├── addressbook_pb2.pyc
├── addressbook.pb.cc
├── addressbook.pb.h
├── addressbook.proto
├── reader
├── reader.cc
└── writer.py
0 directories, 8 files
参考Google官方文档,包含proto文件,及Python和C++示例程序。