今天了解了一下protocal buffer,觉得这个协议还是比较有实用价值的。不同语言(当前只支持java,python和c++)可以对相同的模型进行操作,而且通过特殊的编码使得交互的数据量变得很小。当然,目前对这个协议了解得比较有限,还说不出什么一二三。下面是安装使用protocal buffer的过程。
以下的操作都是在cygwin环境下面。
首先当然是安装。
参照readme文件:
$ ./configure
$ make
$ make check
$ make install
安装之后,进行检查,输入
protoc --version
我这里的结果是:
libprotoc 2.4.0
出现了版本号之后,就说明安装是正常的。
java 接着是,生成jar包:
mvn install
安装 python包:
$ python setup.py install
全部完成之后,采用protocal buffer的例子(此处略去1000字)
分别输出成java文件和py文件
$ protoc -I=. --java_out=. ./addressbook.proto
$ protoc -I=. --python_out=. ./addressbook.proto
用Java添加信息到文件中:
import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
class AddPerson {
// This function fills in a Person message based on user input.
static Person PromptForAddress(BufferedReader stdin,
PrintStream stdout) throws IOException {
Person.Builder person = Person.newBuilder();
stdout.print("Enter person ID: ");
person.setId(Integer.valueOf(stdin.readLine()));
stdout.print("Enter name: ");
person.setName(stdin.readLine());
stdout.print("Enter email address (blank for none): ");
String email = stdin.readLine();
if (email.length() > 0) {
person.setEmail(email);
}
while (true) {
stdout.print("Enter a phone number (or leave blank to finish): ");
String number = stdin.readLine();
if (number.length() == 0) {
break;
}
Person.PhoneNumber.Builder phoneNumber =
Person.PhoneNumber.newBuilder().setNumber(number);
stdout.print("Is this a mobile, home, or work phone? ");
String type = stdin.readLine();
if (type.equals("mobile")) {
phoneNumber.setType(Person.PhoneType.MOBILE);
} else if (type.equals("home")) {
phoneNumber.setType(Person.PhoneType.HOME);
} else if (type.equals("work")) {
phoneNumber.setType(Person.PhoneType.WORK);
} else {
stdout.println("Unknown phone type. Using default.");
}
person.addPhone(phoneNumber);
}
return person.build();
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: AddPerson ADDRESS_BOOK_FILE");
System.exit(-1);
}
AddressBook.Builder addressBook = AddressBook.newBuilder();
// Read the existing address book.
try {
addressBook.mergeFrom(new FileInputStream(args[0]));
} catch (FileNotFoundException e) {
System.out.println(args[0] + ": File not found. Creating a new file.");
}
// Add an address.
addressBook.addPerson(
PromptForAddress(new BufferedReader(new InputStreamReader(System.in)),
System.out));
// Write the new address book back to disk.
FileOutputStream output = new FileOutputStream(args[0]);
addressBook.build().writeTo(output);
output.close();
}
}
编译执行。。。。。。
之后用python读取
import addressbook_pb2
import sys
# Iterates though all people in the AddressBook and prints info about them.
def ListPeople(address_book):
for person in address_book.person:
print "Person ID:", person.id
print " Name:", person.name
if person.HasField('email'):
print " E-mail address:", person.email
for phone_number in person.phone:
if phone_number.type == addressbook_pb2.Person.MOBILE:
print " Mobile phone #: ",
elif phone_number.type == addressbook_pb2.Person.HOME:
print " Home phone #: ",
elif phone_number.type == addressbook_pb2.Person.WORK:
print " Work phone #: ",
print phone_number.number
# Main procedure: Reads the entire address book from a file and prints all
# the information inside.
if len(sys.argv) != 2:
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
sys.exit(-1)
address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
ListPeople(address_book)
最后的结果如图:
添加
读取
- 大小: 31.2 KB
- 大小: 38.4 KB
分享到:
相关推荐
protocolbuffer案例代码
protocol buffer jar架包和Windows下的代码生成工具(protobuf-java-2.5.0.jar protoc-2.5.0-win32)
ProtocolBuffer的exe,源码,及maven
包含protobuf-java-2.5.0.jar、 protoc.exe 、msg.proto、Compile.bat可供生成java和c++的协议文件。使用时可修改msg.proto文件定义自己的协议格式。只需要点击Compile.bat即可生成Msg.java和msg.pb.h msg.pb.cc三个...
本人从Github下载源码编译的完整版(不是lite版),正在项目中使用。android、java都可用
用Java语言实现的利用Protocol Buffer进行数据保存的实例,具体的体现是一个学生记录管理。一个Demo,为了帮大家理解Protocol Buffer的。
Protocol Buffer 2.5.0 jar包
Protocol Buffer文件编译成Java文件所需要的相关工具,jar包,exe编译工具等。
Protocol Buffer使用
C++实例Protocol Buffer技术详解,感兴趣的朋友可以了解下
Protocol buffers 据说是不分平台,比XML更小更快更简洁,具体的描述可以上https://developers.google.com/protocol-buffers/查看
01 Protocol Buffer技术详解(语言规范).doc
Android端中使用Netty+Protocol Buffer实现聊天室功能
Protocol_Buffer官网文档中文版
Protocol Buffer sublime text 3插件,官方网址: https://github.com/vihangm/sublime-protobuf-syntax
protocolbuffer技术用到的源码包及安装包
Protocol Buffer序列化对比Java序列化.
尽管其功能和用途与XML基本相似,但是 Protocol Buffers更为轻便。Google宣称其效率对于XML有很大提升,文本比XML要小3-10倍,而解析效率却提升至20-100倍。Protocol Buffers是一个平台中立,编程语言无关的,可扩展...