protobuf安装和简单使用

安装

在网站 http://code.google.com/p/protobuf/downloads/list上可以下载 Protobuf 的源代码。然后解压编译安装便可以使用它了。

安装步骤如下所示:

##yum组,找到Development tools
yum grouplist 
##查看Development tools
yum groupinfo Development tools
##安装组,组内所有依赖将被安装
yum groupinstall Development tools -y
##解压protobuf压缩包
tar -xzf protobuf-2.5.0.tar.gz 
##进入protobuf解压文件中
cd protobuf-2.5.0
##对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系
./configure 
##编译并安装
make && make install
##查看安装目录
whereis protoc

编写一个proto 文件,后缀名必须为 .proto,放在 /root 目录下

package com.hbase.test; 
 message callLogs 
 { 
     required string  duiFangPhoneNum=1;//对方手机号
     required string  ctime=2;//通话时间
     required string  calltime=3;//通话时长
     required string  type=4;//类型 0主叫  被叫    
 }

在上例中,package 名字叫做 com.hbase.test ,定义了一个消息 callLogs (类名),该消息有4个成员(字段),类型为string。required 必选,而optional 是一个可选的成员,即消息中可以不包含该成员。

编译.proto文件

##进入到 /root 目录下
cd ~
##查看protoc帮助文档
/usr/local/bin/protoc --help
##编译 --java_out=OUT_DIR指定文件输出路径, 生成java
/usr/local/bin/protoc  callLogs.proto --java_out=/root/

java中使用

升级使用

 callLogsCollection 中的对象属性 callLogs是一个list 

package com.hbase.test; 
 message callLogs 
 { 
     required string  duiFangPhoneNum=1;//对方手机号
     required string  ctime=2;//通话时间
     required string  calltime=3;//通话时长
     required string  type=4;//类型 0主叫  被叫    
 }
  message callLogsCollection 
 { 
     repeated callLogs  callLogsCollection=1;//嵌套
 }

使用

读取profobuf类型存储的数据

发布了106 篇原创文章 · 获赞 3 · 访问量 6100

猜你喜欢

转载自blog.csdn.net/qq_22049773/article/details/103977418