{"id":1240,"date":"2017-01-05T11:13:06","date_gmt":"2017-01-05T03:13:06","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=1240"},"modified":"2017-01-05T11:13:06","modified_gmt":"2017-01-05T03:13:06","slug":"protobuf%e6%a6%82%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=1240","title":{"rendered":"protobuf\u6982\u8ff0"},"content":{"rendered":"<h1 id=\"protobuf-\u4f7f\u7528\u6559\u7a0b\">\u4ec0\u4e48\u662fprotobuf<\/h1>\n<p>protobuf\u5168\u79f0protocol buffers, \u5b83\u662fgoogle\u4e3a\u4e86\u5e8f\u5217\u5316\u7ed3\u6784\u4f53\u800c\u5f00\u53d1\u7684\u4e00\u79cd\u8de8\u8bed\u8a00\u3001\u8de8\u5e73\u53f0\u3001\u53ef\u62d3\u5c55\u7684\u4e00\u79cd\u534f\u8bae\uff0c\u7c7b\u4f3c\u4e8exml\uff0c\u4f46\u66f4\u5c0f\u3001\u66f4\u5feb\u3001\u66f4\u7b80\u5355\u3002\u4f7f\u7528protobuf\uff0c\u53ea\u8981\u5b9a\u4e49\u4e86\u7ed3\u6784\u4f53\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u7684\u4ee3\u7801\uff0c\u53bb\u751f\u6210\u3001\u89e3\u6790\u7ed3\u6784\u4f53\u3002<!--more--><\/p>\n<h2 id=\"\u5de5\u4f5c\u539f\u7406\">\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u5728protobuf\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>.proto<\/code>\u6587\u4ef6\u6765\u5b9a\u4e49\u7ed3\u6784\u4f53\u3002\u5728<code>.proto<\/code>\u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u4e00\u7cfb\u5217\u7684\u952e\u503c\u5bf9\uff0c\u6765\u8bb0\u5f55\u6570\u636e\u3002\u5982\uff0c\u4e0b\u9762\u5c31\u662f\u4e00\u4e2a<code>.proto<\/code>\u6587\u4ef6\uff0c\u7528\u6765\u63cf\u8ff0\u4e00\u4e2a\u4eba\u7684\u4fe1\u606f:<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs protobuf\">    <span class=\"hljs-class\"><span class=\"hljs-keyword\">message<\/span> <span class=\"hljs-title\">Person<\/span> <\/span>{<br \/>      <span class=\"hljs-keyword\">required<\/span> <span class=\"hljs-built_in\">string<\/span> name = <span class=\"hljs-number\">1<\/span>;<br \/>      <span class=\"hljs-keyword\">required<\/span> <span class=\"hljs-built_in\">int32<\/span> id = <span class=\"hljs-number\">2<\/span>;<br \/>      <span class=\"hljs-keyword\">optional<\/span> <span class=\"hljs-built_in\">string<\/span> email = <span class=\"hljs-number\">3<\/span>;<br \/><br \/>      <span class=\"hljs-class\"><span class=\"hljs-keyword\">enum<\/span> <span class=\"hljs-title\">PhoneType<\/span> <\/span>{<br \/><span class=\"hljs-constant\">        MOBILE<\/span> = <span class=\"hljs-number\">0<\/span>;<br \/><span class=\"hljs-constant\">        HOME<\/span> = <span class=\"hljs-number\">1<\/span>;<br \/><span class=\"hljs-constant\">        WORK<\/span> = <span class=\"hljs-number\">2<\/span>;<br \/>      }<br \/><br \/>      <span class=\"hljs-class\"><span class=\"hljs-keyword\">message<\/span> <span class=\"hljs-title\">PhoneNumber<\/span> <\/span>{<br \/>        <span class=\"hljs-keyword\">required<\/span> <span class=\"hljs-built_in\">string<\/span> number = <span class=\"hljs-number\">1<\/span>;<br \/>        <span class=\"hljs-keyword\">optional<\/span> PhoneType type = <span class=\"hljs-number\">2<\/span> [default = HOME];<br \/>      }<br \/><br \/>      <span class=\"hljs-keyword\">repeated<\/span> PhoneNumber phone = <span class=\"hljs-number\">4<\/span>;<br \/>    }<br \/><\/code><\/pre>\n<p>\u5982\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6bcf\u4e2amessage\u7c7b\u578b\u90fd\u6709\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u4e0d\u91cd\u590d\u7684\u6570\u636e\u5757\uff0c\u6bcf\u4e2a\u6570\u636e\u5757\u90fd\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u5176\u4e2d\u7684\u503c\u53ef\u4ee5\u662f\u6570\u5b57(integer\u6216\u8005float)\uff0cboolean, strings, raw bytes,\u6216\u8005protobuf\u534f\u8bae\u4e2d\u5176\u4ed6\u7684message\u7c7b\u578b(\u7c7b\u4f3c\u4e8e\u5bf9\u8c61\uff0c\u5bf9\u8c61\u4e2d\u53ef\u4ee5\u5305\u542b\u5bf9\u8c61)\u3002\u6570\u636e\u5757\u53ef\u4ee5\u58f0\u660e\u4e3a\u53ef\u9009\u3001\u5fc5\u9009\u6216\u8005\u53ef\u91cd\u590d\u7684\u3002\u66f4\u591a\u5173\u4e8e<code>.proto<\/code>\u7684\u8bed\u6cd5\uff0c\u53c2\u8003<a href=\"https:\/\/developers.google.cn\/protocol-buffers\/docs\/proto\" target=\"_blank\">https:\/\/developers.google.cn\/protocol-buffers\/docs\/proto<\/a>\u3002<\/p>\n<p>\u4e00\u65e6\u5b9a\u4e49\u5b8c<code>.proto<\/code>\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528proto\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fa\u6307\u5b9a\u8bed\u8a00\u7684\u6e90\u7801\uff0c\u5305\u62ecjava, python, c++\u3002\u751f\u6210\u7684\u4ee3\u7801\u4e2d\uff0c\u4f1a\u58f0\u660e\u4e00\u4e2a<code>Person<\/code>\u7c7b, \u901a\u8fc7<code>Person<\/code>\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5e8f\u5217\u5316\u3001\u53cd\u5e8f\u5217\u5316<code>Person<\/code>\u6570\u636e:<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs css\">    <span class=\"hljs-tag\">Person<\/span> <span class=\"hljs-tag\">person<\/span>;<br \/>    <span class=\"hljs-tag\">person<\/span><span class=\"hljs-class\">.set_name<\/span>(\"<span class=\"hljs-tag\">John<\/span> <span class=\"hljs-tag\">Doe<\/span>\");<br \/>    <span class=\"hljs-tag\">person<\/span><span class=\"hljs-class\">.set_id<\/span>(1234);<br \/>    <span class=\"hljs-tag\">person<\/span><span class=\"hljs-class\">.set_email<\/span>(\"<span class=\"hljs-tag\">jdoe<\/span><span class=\"hljs-at_rule\">@<span class=\"hljs-keyword\">example.com\");<\/span><br \/>    fstream <span class=\"hljs-function\">output<\/span>(<span class=\"hljs-string\">\"myfile\"<\/span>, ios::out | ios::binary)<\/span>;<br \/>    <span class=\"hljs-tag\">person<\/span><span class=\"hljs-class\">.SerializeToOstream<\/span>(&amp;<span class=\"hljs-tag\">output<\/span>);<br \/><\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u628a<code>Person<\/code>\u5bf9\u8c61\u5e8f\u5217\u5316\u5230\u6587\u4ef6\u4e2d\u4e86\uff0c\u4e0b\u9762\u6211\u4eec\u53ef\u4ee5\u518d\u628a<code>Persson<\/code>\u5bf9\u8c61\u89e3\u6790\u51fa\u6765:<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs dns\">    fstream input(\"myfile\", ios<span class=\"hljs-number\">::<\/span>in | ios<span class=\"hljs-number\">::b<\/span>inary)<span class=\"hljs-comment\">;<\/span><br \/>    Person person<span class=\"hljs-comment\">;<\/span><br \/>    person.ParseFromIstream(&amp;input)<span class=\"hljs-comment\">;<\/span><br \/>    cout &lt;&lt; \"Name: \" &lt;&lt; person.name() &lt;&lt; endl<span class=\"hljs-comment\">;<\/span><br \/>    cout &lt;&lt; \"E-mail: \" &lt;&lt; person.email() &lt;&lt; endl<span class=\"hljs-comment\">;<\/span><br \/><\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u60f3message\u4e2d\u589e\u52a0\u65b0\u5b57\u6bb5\uff0c\u800c\u4e0d\u7528\u62c5\u5fc3\u517c\u5bb9\u6027\uff1b\u5f53\u89e3\u6790\u8001\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\u4f1a\u81ea\u52a8\u5ffd\u7565\u65b0\u5b57\u6bb5\u3002<\/p>\n<h2 id=\"\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528xml\">\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528xml<\/h2>\n<p>\u548cxml\u76f8\u6bd4\uff0cprotobuf\u6709\u5f88\u591a\u4f18\u70b9:<\/p>\n<ul>\n<li>\u66f4\u7b80\u5355<\/li>\n<li>\u6bd4xml\uff0c\u5c0f3-10\u500d<\/li>\n<li>\u6bd4xml\uff0c\u5feb20-100\u500d<\/li>\n<li>\u66f4\u4e0d\u5bb9\u6613\u4ea7\u751f\u6b67\u4e49<\/li>\n<li>\u4f7f\u7528\u751f\u6210\u7684\u6e90\u7801\uff0c\u64cd\u4f5c\u7c7b\uff0c\u5bf9\u5f00\u53d1\u8005\u66f4\u53cb\u597d<\/li>\n<\/ul>\n<p>\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u4e00\u4e2a<code>person<\/code>\u6709<code>name<\/code>\u548c<code>email<\/code>\u5c5e\u6027\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49xml\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">person<\/span>&gt;<\/span><br \/>    <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">name<\/span>&gt;<\/span>John Doe<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">name<\/span>&gt;<\/span><br \/>    <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">email<\/span>&gt;<\/span>jdoe@example.com<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">email<\/span>&gt;<\/span><br \/><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">person<\/span>&gt;<\/span><br \/><\/code><\/pre>\n<p>\u800c\u4f7f\u7528protobuf\uff0c\u5982\u4e0b\uff08protobuf\u7684\u6587\u672c\u683c\u5f0f\uff09<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs css\">    #\u6ce8\u610f\u771f\u6b63\u7684<span class=\"hljs-tag\">protobuf<\/span>\u6570\u636e\u662f\u4e8c\u8fdb\u5236\u6570\u636e\u6d41<br \/>    <span class=\"hljs-tag\">person<\/span> <span class=\"hljs-rules\">{<br \/>      <span class=\"hljs-rule\"><span class=\"hljs-attribute\">name<\/span>:<span class=\"hljs-value\"> <span class=\"hljs-string\">\"John Doe\"<\/span><br \/>      email: <span class=\"hljs-string\">\"jdoe@example.com\"<\/span><br \/>    <\/span><\/span><\/span>}<br \/><\/code><\/pre>\n<p>\u5f53\u4e0a\u9762\u7684\u6570\u636e\u88ab\u7f16\u7801\u6210protobuf\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\u53ea\u670928\u5b57\u8282\uff1b\u53cd\u5e8f\u5217\u5316\uff0c\u89e3\u6790\u6210\u5bf9\u8c61\u9700\u8981100-200\u7eb3\u79d2\u3002\u800cxml\u6570\u636e\uff0c\u9700\u898169\u5b57\u8282\uff1b\u53cd\u5e8f\u5217\u5316\u9700\u89815000-10000\u7eb3\u79d2\u3002<\/p>\n<p>\u5e76\u4e14\u64cd\u4f5cprotobuf\u4e5f\u5f88\u7b80\u5355\uff1a<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs cpp\">    <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Name: \"<\/span> &lt;&lt; person.name() &lt;&lt; endl;<br \/>    <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"E-mail: \"<\/span> &lt;&lt; person.email() &lt;&lt; endl;<br \/><\/code><\/pre>\n<p>\u5982\u679c\u662fxml\uff0c\u6211\u4eec\u9700\u8981\uff1a<\/p>\n<pre class=\"prettyprint hljs-dark\"><code class=\"hljs livescript\">    cout &lt;&lt; <span class=\"hljs-string\">\"Name: \"<\/span><br \/>         &lt;&lt; person.getElementsByTagName<span class=\"hljs-function\"><span class=\"hljs-params\">(<span class=\"hljs-string\">\"name\"<\/span>)<\/span>-&gt;<\/span>item<span class=\"hljs-function\"><span class=\"hljs-params\">(<span class=\"hljs-number\">0<\/span>)<\/span>-&gt;<\/span>innerText()<br \/>         &lt;&lt; endl;<br \/>  cout &lt;&lt; <span class=\"hljs-string\">\"E-mail: \"<\/span><br \/>       &lt;&lt; person.getElementsByTagName<span class=\"hljs-function\"><span class=\"hljs-params\">(<span class=\"hljs-string\">\"email\"<\/span>)<\/span>-&gt;<\/span>item<span class=\"hljs-function\"><span class=\"hljs-params\">(<span class=\"hljs-number\">0<\/span>)<\/span>-&gt;<\/span>innerText()<br \/>       &lt;&lt; endl;<br \/><\/code><\/pre>\n<p>\u5f53\u7136\uff0cprotobuf\u4e5f\u4e0d\u90fd\u662f\u6bd4xml\u5f3a\u3002protobuf\u6570\u636e\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u65b9\u5f0f\u5b58\u50a8\u7684\uff0c\u4eba\u7c7b\u96be\u4ee5\u9605\u8bfb\u3002\u800cxml\u662f\u81ea\u63cf\u8ff0\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u7406\u89e3\u5176\u4e2d\u7684\u542b\u4e49\uff1b\u9664\u975e\u6211\u4eec\u62ff\u5230<code>.proto<\/code>\u6587\u4ef6\uff0c\u5426\u5219\u662f\u5f88\u96be\u77e5\u9053protobuf\u6570\u636e\u7684\u542b\u4e49\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fprotobuf protobuf\u5168\u79f0protocol buffers, \u5b83\u662fgoogle\u4e3a\u4e86\u5e8f\u5217\u5316\u7ed3\u6784\u4f53\u800c\u5f00\u53d1\u7684\u4e00\u79cd\u8de8\u8bed\u8a00\u3001\u8de8\u5e73\u53f0\u3001\u53ef\u62d3\u5c55\u7684\u4e00\u79cd\u534f\u8bae\uff0c\u7c7b\u4f3c\u4e8exml\uff0c\u4f46\u66f4\u5c0f\u3001\u66f4\u5feb\u3001\u66f4\u7b80\u5355\u3002\u4f7f\u7528p&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[69,68,5],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-coding","tag-google","tag-protobuf","tag-5"],"views":4615,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1240"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions"}],"predecessor-version":[{"id":1241,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions\/1241"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}