{"id":442,"date":"2012-11-30T05:40:06","date_gmt":"2012-11-30T05:40:06","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=442"},"modified":"2012-12-12T11:46:19","modified_gmt":"2012-12-12T11:46:19","slug":"%e6%b8%b8%e6%88%8f%e5%bc%80%e5%8f%91%e5%87%86%e5%a4%87%e4%b8%89%ef%bc%8d%ef%bc%8d%e4%bc%a0%e8%be%93%e5%8d%8f%e8%ae%ae","status":"publish","type":"post","link":"https:\/\/blog.zhukunqian.com\/?p=442","title":{"rendered":"\u6e38\u620f\u5f00\u53d1\u51c6\u5907(\u4e09)\uff0d\uff0d\u4f20\u8f93\u534f\u8bae"},"content":{"rendered":"<p>google \u7684 protobuf \u662f\u6700\u8fd1\u975e\u5e38\u706b\u7684\u5c06\u7ed3\u6784\u5bf9\u8c61\u8fdb\u884c\u5e8f\u5217\u5316\u53ca\u53cd\u5e8f\u5217\u5316\u7684\u5de5\u5177\uff0c\u636egoogle\u79f0\uff0c\u5df2\u7ecf\u5e94\u7528\u5728\u4e86google\u975e\u5e38\u591a\u7684\u5185\u90e8\u9879\u76ee\u4e0a\u3002<\/p>\n<p>\uff11\u3001\u4e0b\u8f7dprotobuf,protoc:<\/p>\n<p>https:\/\/code.google.com\/p\/protobuf\/downloads\/detail?name=protobuf-2.4.1.tar.bz2&#038;can=2&#038;q=<\/p>\n<p>https:\/\/code.google.com\/p\/protobuf\/downloads\/detail?name=protoc-2.4.1-win32.zip&#038;can=2&#038;q=<\/p>\n<p>\uff12\u3001\u4e0b\u8f7d\u4e0b\u6765\u7684protoc\u653e\u5728 protobuf\/java\/src\u76ee\u5f55\u4e0b<\/p>\n<p>\uff13\u3001\u5728protobuf\/java\u76ee\u5f55\u4e0b\u4fee\u6539pom.xml(\u4ec5windows\u4e0b\u9700\u8981\u4fee\u6539)<\/p>\n<p>&lt;exec executable=&#8221;..\/src\/protoc&#8221;&gt;<\/p>\n<p>\u4fee\u6539\u4e3a\uff1a<\/p>\n<p>&lt;exec executable=&#8221;src\\protoc.exe&#8221;&gt;<\/p>\n<p>\uff14\u3001\u6267\u884c\uff1a<\/p>\n<p>mvn test<\/p>\n<p>mvn install \u5b89\u88c5\u81f3 maven repository<\/p>\n<p>mvn package \u53ef\u4ee5\u6253\u5305\u6210jar\u5305\uff0c\u4f9b\u5355\u72ec\u4f7f\u7528<\/p>\n<p>mvn install -P lite \u751f\u6210lite\u7248\u672c\uff0clite\u7f3a\u5c11\u63cf\u8ff0\u53ca\u53cd\u5c04\u529f\u80fd\uff0c\u4f46\u5728\u624b\u673a\u5e73\u53f0\u4e0a\u63a8\u8350\u4f7f\u7528<\/p>\n<p>\u8fd9\u91cc\u6211\u4e5f\u6253\u5305\u4e86jar\u5305\uff0c\u7528\u6765\u5b66\u4e60\u65f6\u4f7f\u7528\u3002<\/p>\n<p>\uff15\u3001\u5728eclipse\u4e2d\u521b\u5efajava\u5de5\u7a0b\uff0c\u6765\u5b66\u4e60\u4e00\u4e0bprotobuf\u7684\u4f7f\u7528<\/p>\n<p>\u5efa\u7acbaddressbook.proto<\/p>\n<p>option java_package = &#8220;com.example.tutorial&#8221;;<\/p>\n<p>message Person {<br \/>\nrequired string name = 1;<br \/>\nrequired int32 id = 2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Unique ID number for this person.<br \/>\noptional string email = 3;<\/p>\n<p>enum PhoneType {<br \/>\nMOBILE = 0;<br \/>\nHOME = 1;<br \/>\nWORK = 2;<br \/>\n}<\/p>\n<p>message PhoneNumber {<br \/>\nrequired string number = 1;<br \/>\noptional PhoneType type = 2 [default = HOME];<br \/>\n}<\/p>\n<p>repeated PhoneNumber phone = 4;<br \/>\n}<\/p>\n<p>\/\/ Our address book file is just one of these.<br \/>\nmessage AddressBook {<br \/>\nrepeated Person person = 1;<br \/>\n}<\/p>\n<p>\u6267\u884c\uff1a<\/p>\n<p>protoc addressbook &#8211;java-out=.<\/p>\n<p>\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n<p>Person p = Person.newBuilder().setName(&#8220;hello world&#8221;).setId(111)<br \/>\n.build();<br \/>\nSystem.out.println(p.toByteArray().length);<\/p>\n<p>toByteArray()\u8fdb\u884c\u5e8f\u5217\u5316\uff0cparseFrom()\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u4e0b\u9762\u5728maven\u4e2d\u96c6\u6210:<\/p>\n<p>\u6dfb\u52a0plugin:<\/p>\n<p>&lt;plugin&gt;<br \/>\n&lt;artifactId&gt;maven-antrun-plugin&lt;\/artifactId&gt;<br \/>\n&lt;executions&gt;<br \/>\n&lt;execution&gt;<br \/>\n&lt;id&gt;generate-sources&lt;\/id&gt;<br \/>\n&lt;phase&gt;generate-sources&lt;\/phase&gt;<br \/>\n&lt;configuration&gt;<br \/>\n&lt;tasks&gt;<br \/>\n&lt;mkdir dir=&#8221;target\/generated-sources&#8221; \/&gt;<br \/>\n&lt;exec executable=&#8221;protoc&#8221;&gt;<br \/>\n&lt;arg<br \/>\nvalue=&#8221;&#8211;java_out=target\/generated-sources&#8221; \/&gt;<br \/>\n&lt;arg<br \/>\nvalue=&#8221;src\/main\/protobuf\/test.proto&#8221; \/&gt;<br \/>\n&lt;\/exec&gt;<br \/>\n&lt;\/tasks&gt;<br \/>\n&lt;sourceRoot&gt;<br \/>\ntarget\/generated-sources<br \/>\n&lt;\/sourceRoot&gt;<br \/>\n&lt;\/configuration&gt;<br \/>\n&lt;goals&gt;<br \/>\n&lt;goal&gt;run&lt;\/goal&gt;<br \/>\n&lt;\/goals&gt;<br \/>\n&lt;\/execution&gt;<br \/>\n&lt;\/executions&gt;<br \/>\n&lt;\/plugin&gt;<\/p>\n<p>\u6dfb\u52a0dependency:<\/p>\n<p>&lt;dependency&gt;<br \/>\n&lt;groupId&gt;com.google.protobuf&lt;\/groupId&gt;<br \/>\n&lt;artifactId&gt;protobuf-java&lt;\/artifactId&gt;<br \/>\n&lt;version&gt;2.4.1&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<p>\u91cc\u9762\u6709\u4e00\u4e2a\u53cd\u5c04\u673a\u5236\uff0c\u9700\u8981\u67e5\u4e00\u4e0b\u80fd\u8d77\u5230\u4ec0\u4e48\u4f5c\u7528<\/p>\n<p><code>optimize_for \u8fd9\u4e2a\u9009\u9879\u9700\u8981\u5173\u6ce8\u4e00\u4e0b\uff0c\u5982\u679c\u5f00\u53d1\u624b\u673a\u7f51\u6e38\uff0c\u8fd9\u4e2a\u9009\u9879\u4f1a\u6709\u5927\u7528\u5904\u3002<\/code><\/p>\n<p>option optimize_for = SPEED; \/\/ \u9ed8\u8ba4<\/p>\n<p><code>CODE_SIZE<\/code><\/p>\n<p><code>LITE_RUNTIME \/\/\u624b\u673a\u7f51\u6e38\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd<br \/>\n<\/code><\/p>\n<p>\u53e6\u5916\uff0c\u9700\u8981\u68c0\u67e5\u4e00\u4e0bprotobuf\u7684\u538b\u7f29\u3002\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u5b57\u7b26\u4e32\u597d\u50cf\u5e76\u672a\u538b\u7f29\u3002<\/p>\n<p>\u4ecesmartfox\u4e0a\u67e5\u627e\u4e00\u4e0b\u4ed6\u4eec\u7528\u7684\u538b\u7f29\u5de5\u5177\uff0c\u7ee7\u7eed\u6d4b\u8bd5\u4e00\u4e0b\u3002<\/p>\n<p>\u51b3\u5b9a\u4f7f\u7528snappy\u7b97\u6cd5\u3002<\/p>\n<p>https:\/\/github.com\/dain\/snappy<\/p>\n<p>\u5404\u79cd\u538b\u7f29\u7b97\u6cd5\u5728java\u5e73\u53f0\u4e0a\u7684\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n<p>\u5b9e\u9645\u6d4b\u8bd5\u540e\u538b\u7f29\u6548\u679c\u4e0d\u5c3d\u5982\u4eba\u610f\uff0c\u4f7f\u7528\u4e86deflate\u548csnappy\u538b\u7f29\u7b97\u6cd5\uff0c\u5206\u522b\u6d4b\u8bd5\u4e86\u5c0f\u4e8e\uff12\uff10\u5b57\u8282\u548c\uff11\uff12\uff10\u5b57\u8282\u7684\u538b\u7f29\uff0c\u53ea\u80fd\u538b\u7f29\u4e0d\u5230\uff12\uff10\uff05\uff0c\u521d\u59cb\u6000\u7591protobuf\u7684\u4e8c\u8fdb\u5236\u5df2\u7ecf\u5145\u5206\u4f18\u5316\u8fc7\u4e86\uff0c\u5982\u679c\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u518d\u6d88\u8017cpu\u8fdb\u884c\u538b\u7f29\u5c31\u6ca1\u6709\u5fc5\u8981\u4e86\u3002\u4f46\u662f\u4e5f\u4e0d\u6392\u9664\u6d4b\u8bd5\u7684\u6837\u672c\u592a\u5c11\u3002\u53ef\u4ee5\u7b49\u6b63\u5f0f\u4e0a\u7ebf\u540e\uff0c\u518d\u56de\u5934\u8fdb\u884c\u538b\u7f29\u7684\u6d4b\u8bd5\u3002\u5bf9\u4e86\uff0csnappy\u5fc5\u987b\u5728jdk7\u4e0b\u8fd0\u884c\uff0c\u56e0\u4e3a\u7528\u4e86\u5230sun\u7684\u975e\u516c\u5f00\u51fd\u6570\uff0c\u6240\u4ee5\u9700\u8981\u6ce8\u610f\u4e00\u4e0b\u662f\u76ee\u6807\u73af\u5883\u662f\u5426\u652f\u6301\u3002<\/p>\n<p>\u4f7f\u7528protobuf\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528java\u53cd\u5c04\u673a\u5236\u5e26\u6765\u7684\u6548\u7387\u964d\u4f4e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>google \u7684 protobuf \u662f\u6700\u8fd1\u975e\u5e38\u706b\u7684\u5c06\u7ed3\u6784\u5bf9\u8c61\u8fdb\u884c\u5e8f\u5217\u5316\u53ca\u53cd\u5e8f\u5217\u5316 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/blog.zhukunqian.com\/?p=442\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/442"}],"collection":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442"}],"version-history":[{"count":14,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":445,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions\/445"}],"wp:attachment":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}