文档章节

protobuf实例-PHP版

mickelfeng
 mickelfeng
发布于 2013/03/24 12:07
字数 1627
阅读 2.1K
收藏 0

#程序员薪资揭榜#你做程序员几年了?月薪多少?发量还在么?>>>

protobuf实例-PHP版

protobuf简介
protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多。虽然是二进制数据格式,但并没有因此变得复杂,开发人员通过按照一定的语法定义结构化的消息格式,然后送给命令行工具,工具将自动生成相关的类,可以支持php、java、c++、python等语言环境。通过将这些类包含在项目中,可以很轻松的调用相关方法来完成业务消息的序列化与反序列化工作。

protobuf在google中是一个比较核心的基础库,作为分布式运算涉及到大量的不同业务消息的传递,如何高效简洁的表示、操作这些业务消息在google这样的大规模应用中是至关重要的。而protobuf这样的库正好是在效率、数据大小、易用性之间取得了很好的平衡。

更多信息可参考官方文档
http://code.google.com/p/protobuf/

http://code.google.com/p/pb4php/(这是PHP类库)以下是具体操作例子

[php-protobuf](https://github.com/allegro/php-protobuf) PHP Protobuf - Google's Protocol Buffers for PHP

<?php
require_once('parser/pb_parser.php'); $test = new PBParser(); $test->parse('./test.proto');
 
require_once('message/pb_message.php');
require_once('pb_proto_test.php');
$string = file_get_contents('./example/test.pb');
 
$book = new AddressBook();
$person = $book->add_person();
$person->set_name('Kordulla');
$person->set_surname('Nikolai');
 
$phone_number = $person->add_phone();
$phone_number->set_number('49');
$phone_number->set_type(Person_PhoneType::WORK);
 
$phone_number = $person->add_phone();
$phone_number->set_number('171');
$phone_number->set_type(Person_PhoneType::MOBILE);
 
// serialize
$string = $book->SerializeToString();
//echo $string;
// write it to disk
file_put_contents('adressbook.pb', $string);
$string = file_get_contents('./adressbook.pb');
// Just read it
$book = new AddressBook();
$book->parseFromString($string);
 
var_dump($book->person_size());
$person = $book->person(0);
var_dump($person->name());
var_dump($person->surname());
var_dump($person->phone(0)->number());
var_dump($person->phone(0)->type());
var_dump($person->phone(1)->number());
var_dump($person->phone(1)->type());

输出

int(1) string(8) "Kordulla" string(7) "Nikolai" string(2) "49" int(2) string(3) "171" int(0)
“adressbook.pb” 是生成的二进制文件 基本结构一个字节类型+ 字节长度
从以上操作和类库源代码来看打包速度可能慢很多。 空间节省倒是非常好。符合 protobuf 定义:效率、数据大小、易用性之间的平衡。

在网上也可以搜索到xml、json、protobuf 对比的性能测试。

http://blog.hucde.com/2011/07/20/180

PHP Protobuf - Google's Protocol Buffers for PHP

Overview

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. It might be used in file formats and RPC protocols.

PHP Protobuf is Google's Protocol Buffers implementation for PHP with a goal to provide high performance, including a protoc plugin to generate PHP classes from .proto files. The heavy-lifting (a parsing and a serialization) is done by a PHP extension.

Requirements

  • PHP 7.0 or above (for PHP 5 support refer to php5 branch)
  • Pear's Console_CommandLine (for the protoc plugin)
  • Google's protoc compiler version 2.6 or above

Getting started

Installation

  1. Clone the source code

    git clone https://github.com/allegro/php-protobuf
    
  2. Go to the source code directory

    cd php-protobuf
    
  3. Build and install the PHP extension (follow instructions at php.net)

  4. Install protoc plugin dependencies

    composer install
    

Usage

  1. Assume you have a file foo.proto

    message Foo
    {
        required int32 bar = 1;
        optional string baz = 2;
        repeated float spam = 3;
    }
    
  2. Compile foo.proto

    php protoc-gen-php.php foo.proto
    
  3. Create Foo message and populate it with some data

    require_once 'Foo.php';
    
    $foo = new Foo();
    $foo->setBar(1);
    $foo->setBaz('two');
    $foo->appendSpam(3.0);
    $foo->appendSpam(4.0);
  4. Serialize a message to a string

    $packed = $foo->serializeToString();
  5. Parse a message from a string

    $parsedFoo = new Foo();
    try {
        $parsedFoo->parseFromString($packed);
    } catch (Exception $ex) {
        die('Oops.. there is a bug in this example, ' . $ex->getMessage());
    }
  6. Let's see what we parsed out

    $parsedFoo->dump();

    It should produce output similar to the following:

    Foo {
      1: bar => 1
      2: baz => 'two'
      3: spam(2) =>
        [0] => 3
        [1] => 4
    }
    
  7. If you would like you can reset an object to its initial state

    $parsedFoo->reset();

Guide

Compilation

PHP Protobuf comes with Google's protoc compiler plugin. You can run in directly:

php protoc-gen-php.php -o output_dir foo.proto

or pass it to the protoc:

protoc --plugin=protoc-gen-allegrophp=protoc-gen-php.php --allegrophp_out=output_dir foo.proto

On Windows use protoc-gen-php.bat instead.

Command line options

  • -o out, --out=out - the destination directory for generated files (defaults to the current directory).
  • -I proto_path, --proto_path=proto_path - the directory in which to search for imports.
  • --protoc=protoc - the protoc compiler executable path.
  • -D define, --define=define - define a generator option (i.e. -Dnamespace='Foo\Bar\Baz').

Generator options

  • namespace - the namespace to be used by the generated PHP classes.

Message class

The classes generated during the compilation are PSR-0 compliant (each class is put into it's own file). If namespace generator option is not defined then a package name (if present) is used to create a namespace. If the package name is not set then a class is put into global space.

PHP Protobuf module implements ProtobufMessage class which encapsulates the protocol logic. A message compiled from a proto file extends this class providing message field descriptors. Based on these descriptors ProtobufMessage knows how to parse and serialize a message of a given type.

For each field a set of accessors is generated. The set of methods is different for single value fields (required / optional) and multi-value fields (repeated).

  • required / optional

      get{FIELD}()        // return field value
      set{FIELD}($value)  // set field value to $value
    
  • repeated

      append{FIELD}($value)       // append $value value to field
      clear{FIELD}()              // empty field
      get{FIELD}()                // return array of field values
      getAt{FIELD}($index)        // return field value at $index index
      getCount{FIELD}()           // return number of field values
      getIterator{FIELD}($index)  // return ArrayIterator for field values
    

{FIELD} is a camel cased field name.

Enum

PHP does not natively support enum type. Hence enum is represented by the PHP integer type. For convenience enum is compiled to a class with set of constants corresponding to its possible values.

Type mapping

The range of available build-in PHP types poses some limitations. PHP does not support 64-bit positive integer type. Note that parsing big integer values might result in getting unexpected results.

Protocol Buffers types map to PHP types as follows (x86_64):

| Protocol Buffers | PHP    |
| ---------------- | ------ |
| double           | float  |
| float            |        |
| ---------------- | ------ |
| int32            | int    |
| int64            |        |
| uint32           |        |
| uint64           |        |
| sint32           |        |
| sint64           |        |
| fixed32          |        |
| fixed64          |        |
| sfixed32         |        |
| sfixed64         |        |
| ---------------- | ------ |
| bool             | bool   |
| ---------------- | ------ |
| string           | string |
| bytes            |        |

Protocol Buffers types map to PHP types as follows (x86):

| Protocol Buffers | PHP                         |
| ---------------- | --------------------------- |
| double           | float                       |
| float            |                             |
| ---------------- | --------------------------- |
| int32            | int                         |
| uint32           |                             |
| sint32           |                             |
| fixed32          |                             |
| sfixed32         |                             |
| ---------------- | --------------------------- |
| int64            | if val <= PHP_INT_MAX       |
| uint64           | then value is stored as int |
| sint64           | otherwise as double         |
| fixed64          |                             |
| sfixed64         |                             |
| ---------------- | --------------------------- |
| bool             | bool                        |
| ---------------- | --------------------------- |
| string           | string                      |
| bytes            |                             |

Not set value is represented by null type. To unset value just set its value to null.

Parsing

To parse message create a message class instance and call its parseFromString method passing it a serialized message. The errors encountered are signaled by throwing Exception. Exception message provides detailed explanation. Required fields not set are silently ignored.

$packed = /* serialized FooMessage */;
$foo = new FooMessage();

try {
    $foo->parseFromString($packed);
} catch (Exception $ex) {
    die('Parse error: ' . $e->getMessage());
}

$foo->dump(); // see what you got

Serialization

To serialize a message call serializeToString method. It returns a string containing protobuf-encoded message. The errors encountered are signaled by throwing Exception. Exception message provides detailed explanation. A required field not set triggers an error.

$foo = new FooMessage()
$foo->setBar(1);

try {
    $packed = $foo->serializeToString();
} catch (Exception $ex) {
    die 'Serialize error: ' . $e->getMessage();
}

/* do some cool stuff with protobuf-encoded $packed */

Debugging

There might be situations you need to investigate what an actual content of a given message is. What var_dump gives on a message instance is somewhat obscure.

The ProtobufMessage class comes with dump method which prints out a message content to the standard output. It takes one optional argument specifying whether you want to dump only set fields (by default it dumps only set fields). Pass falseas an argument to dump all fields. Format it produces is similar to var_dump.

Alternatively you can use printDebugString() method which produces output in protocol buffers text format.

IDE Helper and Auto-Complete Support

To integrate this extension with your IDE (PhpStorm, Eclipse etc.) and get auto-complete support, simply include stubs\ProtobufMessage.php anywhere under your project root.

Known issues

References

Acknowledgments

© 著作权归作者所有

mickelfeng

mickelfeng

粉丝 238
博文 2811
码字总数 607897
作品 0
成都
高级程序员
私信 提问
加载中

评论(2)

不要点击我
不要点击我
我使用了它,但是有一个问题,它不支持三层嵌套的数据!!! 可实用性太差啦!!!
还有就是长期不更新!现在一个项目有4层的嵌套数据!!有什么好点的方法嘛???
eleven0220
eleven0220
为什么不放开????
Protobuf 安装及 Python、C# 示例

01| 简介02| 安装2.1 Windows 下安装03| 简单使用3.1 编译3.2 Python 示例3.3 C# 示例 01| 简介 Protobuf(Protocol Buffers),是 Google 开发的一种跨语言、跨平台的可扩展机制,用于序列化...

osc_kwrafgul
04/16
7
0
Protobuffer | PHP安装Google protobuf及使用

PHP安装Google protobuf及使用 备注 项目采用redis集群(主从方式)存储数据;数据量月增50W,单个数据序列化情况下达到2k,继续压缩数据解决空间. 项目服务采用PHP(版本5.3)作为RPC服务版本. pro...

云迹
2017/02/05
3K
0
Google protobuf与Socket通信数据流

Google protobuf与Socket通信数据流,IM通信数据流; 1. protobuf的使用与集成,protobuf生成Java Proto文件? 2. android使用wire方式生成protobuf的Java文件? Protobuf Plugin for Gradle...

desaco
2019/01/31
0
0
php protobuf 安装使用

refer: PHP读写protobuf3示例 一.环境   PHP 5.6.30 Ubuntu 16.04 LTS 二.安装protobuf编译器   cd /data/   git clone https://github.com/google/protobuf.git   cd protobuf ......

osc_p1rj1z8j
2018/10/17
3
0
如何使用 Protobuf 做数据交换

在以不同语言编写并在不同平台上运行的应用程序之间交换数据时,Protobuf 编码可提高效率。 协议缓冲区Protocol Buffers(Protobufs)像 XML 和 JSON 一样,可以让用不同语言编写并在不同平台...

作者: Marty Kalin
2019/11/22
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Kubernetes Ingress简单入门

本文转载自Rancher Labs 不知道你是否注意到一个奇怪的现象,尽管Kubernetes Ingress API仍然处于beta状态,但是已经有许多公司使用它来暴露Kubernetes服务。从事相关项目的工程师表示,Kub...

osc_slfjdpj6
6分钟前
8
0
ACCDB MDB Explorer for mac(数据库查看器)

想要更加直观的查看大数据,小编推荐使用mac数据查看工具ACCDB MDB Explorer Mac版,该软件能够将导入的数据库导出为XML,XLS,CSV和TXT格式的文件简便高效请添加链接描述,功能强大,需要的...

osc_63i522q0
8分钟前
0
0
二 javanio中的Channel transfer 内容

java nio中channel操作 分散 将channel中读取的数据分散写入多个buffer中 聚集 将多个buffer写入数据到同一个channel 应用 将消息头和消息体放入到不同buffer,便于分类处理应用 将消息头和消...

writeademo
8分钟前
13
0
如何更好的盘活和用好数据资产,企业如何构建数据竞争力的关键在这里!

在今年4月公布的《中共中央国务院关于构建更加完善的要素市场化配置体系机制的意见》中,“数据”首次作为一种新型生产要素写入中央文件中,与土地、劳动力、资本、技术等传统要素并列成为要...

osc_5asswvor
9分钟前
9
0
如何更好的盘活和用好数据资产,企业如何构建数据竞争力的关键在这里!

在今年4月公布的《中共中央国务院关于构建更加完善的要素市场化配置体系机制的意见》中,“数据”首次作为一种新型生产要素写入中央文件中,与土地、劳动力、资本、技术等传统要素并列成为要...

osc_e7clsgkf
10分钟前
12
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部