{"id":3451,"date":"2021-06-11T15:29:20","date_gmt":"2021-06-11T07:29:20","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=3451"},"modified":"2023-12-04T15:17:28","modified_gmt":"2023-12-04T07:17:28","slug":"protobuf","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=3451","title":{"rendered":"Protobuf"},"content":{"rendered":"<h2>Protobuf\u7b80\u4ecb<\/h2>\n<pre><code>    \u4ec0\u4e48\u662f Google Protocol Buffer\uff1f \u5047\u5982\u60a8\u5728\u7f51\u4e0a\u641c\u7d22\uff0c\u5e94\u8be5\u4f1a\u5f97\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u6587\u5b57\u4ecb\u7ecd\uff1a\n    Google Protocol Buffer( \u7b80\u79f0 Protobuf) \u662f Google \u516c\u53f8\u5185\u90e8\u7684\u6df7\u5408\u8bed\u8a00\u6570\u636e\u6807\u51c6\uff0c\u76ee\u524d\u5df2\u7ecf\u6b63\u5728\u4f7f\u7528\u7684\u6709\u8d85\u8fc7 48,162 \u79cd\u62a5\u6587\u683c\u5f0f\u5b9a\u4e49\u548c\u8d85\u8fc7 12,183 \u4e2a .proto \u6587\u4ef6\u3002\u4ed6\u4eec\u7528\u4e8e RPC \u7cfb\u7edf\u548c\u6301\u7eed\u6570\u636e\u5b58\u50a8\u7cfb\u7edf\u3002\n    Protocol Buffers \u662f\u4e00\u79cd\u8f7b\u4fbf\u9ad8\u6548\u7684\u7ed3\u6784\u5316\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5316\uff0c\u6216\u8005\u8bf4\u5e8f\u5217\u5316\u3002\u5b83\u5f88\u9002\u5408\u505a\u6570\u636e\u5b58\u50a8\u6216 RPC \u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u53ef\u7528\u4e8e\u901a\u8baf\u534f\u8bae\u3001\u6570\u636e\u5b58\u50a8\u7b49\u9886\u57df\u7684\u8bed\u8a00\u65e0\u5173\u3001\u5e73\u53f0\u65e0\u5173\u3001\u53ef\u6269\u5c55\u7684\u5e8f\u5217\u5316\u7ed3\u6784\u6570\u636e\u683c\u5f0f\u3002\u76ee\u524d\u63d0\u4f9b\u4e86 C++\u3001Java\u3001Python \u4e09\u79cd\u8bed\u8a00\u7684 API\u3002\n    \u6216\u8bb8\u60a8\u548c\u6211\u4e00\u6837\uff0c\u5728\u7b2c\u4e00\u6b21\u770b\u5b8c\u8fd9\u4e9b\u4ecb\u7ecd\u540e\u8fd8\u662f\u4e0d\u660e\u767d Protobuf \u7a76\u7adf\u662f\u4ec0\u4e48\uff0c\u90a3\u4e48\u6211\u60f3\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5e94\u8be5\u6bd4\u8f83\u6709\u52a9\u4e8e\u7406\u89e3\u5b83\u3002\n<\/code><\/pre>\n<h2>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<\/h2>\n<h3>\u5b89\u88c5 Google Protocol Buffer<\/h3>\n<p>https:\/\/github.com\/protocolbuffers\/protobuf\/releases\/tag\/v3.17.3<\/p>\n<h4>\u5b89\u88c5\u6b65\u9aa4\u5982\u4e0b\u6240\u793a\uff1a<\/h4>\n<pre><code class=\"language-shell line-numbers\">tar -xzf protobuf-all-3.17.3.tar.gz\ncd protobuf-3.17.3\n.\/configure\nmake &amp;&amp; make install\n<\/code><\/pre>\n<h3>\u5173\u4e8e\u7b80\u5355\u4f8b\u5b50\u7684\u63cf\u8ff0<\/h3>\n<h3>\u4e66\u5199 .proto \u6587\u4ef6<\/h3>\n<p>phone. proto<\/p>\n<pre><code class=\"language-vim line-numbers\">syntax = \"proto2\";\npackage cn.org.wyxxt;\nmessage PhoneDetail {\n    required string dnum = 1;\n    required string length = 2;\n    required string type = 3;\n    required string date = 4;\n}\n<\/code><\/pre>\n<h3>\u7f16\u8bd1 .proto \u6587\u4ef6<\/h3>\n<pre><code class=\"language-shell line-numbers\">\/usr\/local\/bin\/protoc phone.proto -help\n\n\/usr\/local\/bin\/protoc phone.proto --java_out=\/root\/\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_5a0bfcd8ad734eb1baecb895ad011d25.jpg\" alt=\"\" \/><\/p>\n<h3>\u7f16\u5199\u4ee3\u7801<\/h3>\n<pre><code class=\"language-java line-numbers\">@Test\n  public void insertByProtoBuf() throws ParseException, IOException {\n    List&lt;Put&gt; puts = new ArrayList&lt;&gt;();\n    for (int i = 0; i &lt; 10; i++) {\n      String phoneNumber = getNumber(\"158\");\n      for (int j = 0; j &lt; 10000; j++) {\n        String dnum = getNumber(\"177\");\n        String length = String.valueOf(random.nextInt(100));\n        String date = getDate(\"2019\");\n        String type = String.valueOf(random.nextInt(2));\n        \/\/ rowKey\n        String rowKey = phoneNumber + \"_\" + (Long.MAX_VALUE - sdf.parse(date).getTime());\n\n        Phone.PhoneDetail.Builder builder = Phone.PhoneDetail.newBuilder();\n        builder.setDate(date);\n        builder.setDnum(dnum);\n        builder.setLength(length);\n        builder.setType(type);\n        Put put = new Put(Bytes.toBytes(rowKey));\n        put.addColumn(Bytes.toBytes(\"cf\"), Bytes.toBytes(\"phone\"), builder.build().toByteArray());\n        puts.add(put);\n      }\n    }\n    table.put(puts);\n  }\n\n\n@Test\n  public void getByProtoBuf() throws IOException {\n    Get get = new Get(Bytes.toBytes(\"15894400392_9223370487438904807\"));\n    Result rs = table.get(get);\n    byte[] b =\n        CellUtil.cloneValue(rs.getColumnLatestCell(Bytes.toBytes(\"cf\"), Bytes.toBytes(\"phone\")));\n    Phone.PhoneDetail phoneDetail = Phone.PhoneDetail.parseFrom(b);\n    System.out.println(phoneDetail);\n  }\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_cab77c7ee92ecf77995f4e07a2d7cc40.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_eba414ceb829033af8315cd08d885776.jpg\" alt=\"\" \/><\/p>\n<h2>\u548c\u5176\u4ed6\u7c7b\u4f3c\u6280\u672f\u7684\u6bd4\u8f83<\/h2>\n<pre><code>\u770b\u5b8c\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4e4b\u540e\uff0c\u5e0c\u671b\u60a8\u5df2\u7ecf\u80fd\u7406\u89e3 Protobuf \u80fd\u505a\u4ec0\u4e48\u4e86\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u4f1a\u8bf4\uff0c\u4e16\u4e0a\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u7684\u7c7b\u4f3c\u6280\u672f\u554a\uff0c\u6bd4\u5982 XML\uff0cJSON\uff0cThrift \u7b49\u7b49\u3002\u548c\u4ed6\u4eec\u76f8\u6bd4\uff0cProtobuf \u6709\u4ec0\u4e48\u4e0d\u540c\u5462\uff1f\n\u7b80\u5355\u8bf4\u6765 Protobuf \u7684\u4e3b\u8981\u4f18\u70b9\u5c31\u662f\uff1a\u7b80\u5355\uff0c\u5feb\u3002\n\u8fd9\u6709\u6d4b\u8bd5\u4e3a\u8bc1\uff0c\u9879\u76ee thrift-protobuf-compare \u6bd4\u8f83\u4e86\u8fd9\u4e9b\u7c7b\u4f3c\u7684\u6280\u672f\nTotal Time \u6307\u4e00\u4e2a\u5bf9\u8c61\u64cd\u4f5c\u7684\u6574\u4e2a\u65f6\u95f4\uff0c\u5305\u62ec\u521b\u5efa\u5bf9\u8c61\uff0c\u5c06\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5185\u5b58\u4e2d\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u7136\u540e\u518d\u53cd\u5e8f\u5217\u5316\u7684\u6574\u4e2a\u8fc7\u7a0b\u3002\u4ece\u6d4b\u8bd5\u7ed3\u679c\u53ef\u4ee5\u770b\u5230 Protobuf \u7684\u6210\u7ee9\u5f88\u597d\uff0c\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5230\u7f51\u7ad9 http:\/\/code.google.com\/p\/thrift-protobuf-compare\/wiki\/Benchmarking \u4e0a\u4e86\u89e3\u66f4\u8be6\u7ec6\u7684\u6d4b\u8bd5\u7ed3\u679c\u3002\n<\/code><\/pre>\n<h3>Protobuf \u7684\u4f18\u70b9<\/h3>\n<pre><code>Protobuf \u6709\u5982 XML\uff0c\u4e0d\u8fc7\u5b83\u66f4\u5c0f\u3001\u66f4\u5feb\u3001\u4e5f\u66f4\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u4f7f\u7528\u4ee3\u7801\u751f\u6210\u5668\u751f\u6210\u7684\u4ee3\u7801\u6765\u8bfb\u5199\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u3002\u4f60\u751a\u81f3\u53ef\u4ee5\u5728\u65e0\u9700\u91cd\u65b0\u90e8\u7f72\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u66f4\u65b0\u6570\u636e\u7ed3\u6784\u3002\u53ea\u9700\u4f7f\u7528 Protobuf \u5bf9\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u4e00\u6b21\u63cf\u8ff0\uff0c\u5373\u53ef\u5229\u7528\u5404\u79cd\u4e0d\u540c\u8bed\u8a00\u6216\u4ece\u5404\u79cd\u4e0d\u540c\u6570\u636e\u6d41\u4e2d\u5bf9\u4f60\u7684\u7ed3\u6784\u5316\u6570\u636e\u8f7b\u677e\u8bfb\u5199\u3002\n\u5b83\u6709\u4e00\u4e2a\u975e\u5e38\u68d2\u7684\u7279\u6027\uff0c\u5373\u201c\u5411\u540e\u201d\u517c\u5bb9\u6027\u597d\uff0c\u4eba\u4eec\u4e0d\u5fc5\u7834\u574f\u5df2\u90e8\u7f72\u7684\u3001\u4f9d\u9760\u201c\u8001\u201d\u6570\u636e\u683c\u5f0f\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u5bf9\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5347\u7ea7\u3002\u8fd9\u6837\u60a8\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u4e0d\u5fc5\u62c5\u5fc3\u56e0\u4e3a\u6d88\u606f\u7ed3\u6784\u7684\u6539\u53d8\u800c\u9020\u6210\u7684\u5927\u89c4\u6a21\u7684\u4ee3\u7801\u91cd\u6784\u6216\u8005\u8fc1\u79fb\u7684\u95ee\u9898\u3002\u56e0\u4e3a\u6dfb\u52a0\u65b0\u7684\u6d88\u606f\u4e2d\u7684 field \u5e76\u4e0d\u4f1a\u5f15\u8d77\u5df2\u7ecf\u53d1\u5e03\u7684\u7a0b\u5e8f\u7684\u4efb\u4f55\u6539\u53d8\u3002\nProtobuf \u8bed\u4e49\u66f4\u6e05\u6670\uff0c\u65e0\u9700\u7c7b\u4f3c XML \u89e3\u6790\u5668\u7684\u4e1c\u897f\uff08\u56e0\u4e3a Protobuf \u7f16\u8bd1\u5668\u4f1a\u5c06 .proto \u6587\u4ef6\u7f16\u8bd1\u751f\u6210\u5bf9\u5e94\u7684\u6570\u636e\u8bbf\u95ee\u7c7b\u4ee5\u5bf9 Protobuf \u6570\u636e\u8fdb\u884c\u5e8f\u5217\u5316\u3001\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\uff09\u3002\n\u4f7f\u7528 Protobuf \u65e0\u9700\u5b66\u4e60\u590d\u6742\u7684\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff0cProtobuf \u7684\u7f16\u7a0b\u6a21\u5f0f\u6bd4\u8f83\u53cb\u597d\uff0c\u7b80\u5355\u6613\u5b66\uff0c\u540c\u65f6\u5b83\u62e5\u6709\u826f\u597d\u7684\u6587\u6863\u548c\u793a\u4f8b\uff0c\u5bf9\u4e8e\u559c\u6b22\u7b80\u5355\u4e8b\u7269\u7684\u4eba\u4eec\u800c\u8a00\uff0cProtobuf \u6bd4\u5176\u4ed6\u7684\u6280\u672f\u66f4\u52a0\u6709\u5438\u5f15\u529b\u3002\n<\/code><\/pre>\n<h3>Protobuf \u7684\u4e0d\u8db3<\/h3>\n<pre><code>Protbuf \u4e0e XML \u76f8\u6bd4\u4e5f\u6709\u4e0d\u8db3\u4e4b\u5904\u3002\u5b83\u529f\u80fd\u7b80\u5355\uff0c\u65e0\u6cd5\u7528\u6765\u8868\u793a\u590d\u6742\u7684\u6982\u5ff5\u3002\nXML \u5df2\u7ecf\u6210\u4e3a\u591a\u79cd\u884c\u4e1a\u6807\u51c6\u7684\u7f16\u5199\u5de5\u5177\uff0cProtobuf \u53ea\u662f Google \u516c\u53f8\u5185\u90e8\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u5728\u901a\u7528\u6027\u4e0a\u8fd8\u5dee\u5f88\u591a\u3002\n\u7531\u4e8e\u6587\u672c\u5e76\u4e0d\u9002\u5408\u7528\u6765\u63cf\u8ff0\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5 Protobuf \u4e5f\u4e0d\u9002\u5408\u7528\u6765\u5bf9\u57fa\u4e8e\u6587\u672c\u7684\u6807\u8bb0\u6587\u6863\uff08\u5982 HTML\uff09\u5efa\u6a21\u3002\u53e6\u5916\uff0c\u7531\u4e8e XML \u5177\u6709\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u7684\u81ea\u89e3\u91ca\u6027\uff0c\u5b83\u53ef\u4ee5\u88ab\u4eba\u76f4\u63a5\u8bfb\u53d6\u7f16\u8f91\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a Protobuf \u4e0d\u884c\uff0c\u5b83\u4ee5\u4e8c\u8fdb\u5236\u7684\u65b9\u5f0f\u5b58\u50a8\uff0c\u9664\u975e\u4f60\u6709 .proto \u5b9a\u4e49\uff0c\u5426\u5219\u4f60\u6ca1\u6cd5\u76f4\u63a5\u8bfb\u51fa Protobuf \u7684\u4efb\u4f55\u5185\u5bb9\u3002\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Protobuf\u7b80\u4ecb \u4ec0\u4e48\u662f Google Protocol Buffer\uff1f \u5047\u5982\u60a8\u5728\u7f51\u4e0a\u641c\u7d22\uff0c\u5e94\u8be5\u4f1a\u5f97\u5230\u7c7b\u4f3c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[390],"class_list":["post-3451","post","type-post","status-publish","format-standard","hentry","category-15","tag-390"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3451"}],"version-history":[{"count":7,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3451\/revisions"}],"predecessor-version":[{"id":3458,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3451\/revisions\/3458"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}