Protocol Buffersã®ä½¿ãæ¹ã¾ã¨ã
protocol buffersã¨ã¯ãGoogleãèªç¤¾ã®ãµã¼ãã¹ç¨ã«éçºãããªãã¸ã§ã¯ãã·ãªã¢ã©ã¤ãºã®ä»æ§ã§ãã
JSONã«ä¼¼ã¦ãã¾ããã.protoã¨ããååã®ãã¡ã¤ã«ã§æ§é ãå®ç¾©ããJSONã®ãã¼ã¯æ°å¤ã«å¤æãããããã¼ã¿ãµã¤ãºãå°ããå½¢ã§ããã¨ãå¯è½ã§ãã
éçºãããèæ¯ã¯ãGoogleã§ã¯ç¤¾å ã§Javaã ã£ããPythonã ã£ããè¤æ°ã®è¨èªã使ããã¦ããã®ã§ãè¨èªãã¥ã¼ãã©ã«ãªã·ãªã¢ã©ã¤ãºãã¼ã¿ã®ããã¨ãã®ä»çµã¿ãå¿ è¦ã ã£ãã¨ãããã¨ã®ããã§ãã
ããã¥ã¢ã«ã«ã¯ãXML-RPCããããã¼ã¿ãµã¤ãºãå°ãã解æé度ã¯100åæ©ãã¨è¨è¿°ããã¦ãã¾ãã
protocol buffersã使ãå©ç¹ã¨ãã¦ã¯ä»¥ä¸ã®ãããªãã¨ãæãããã¾ã
- ãã¼ã¿ãå°ãã
- ãã¼ã¹ãéã
- è¨èªã«ä¾åããªã(å ¬å¼ã®ã©ã¤ãã©ãªã¯JavaãC++ãPythonã®ã¿ã§ãããRubyã®ã©ã¤ãã©ãªãªã©ãããã¾ã)
ç¾å¨ã¯ããã¼ã¹ãå§ç¸®çãæå©ãªMessagePackãããã¾ãããã»ã¼ä»¥ä¸ã®ãã¥ã¼ããªã¢ã«ã®ã¾ã¾ã§ãããã¨ãããããµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ãåä½ãããã¾ã§ã®æé ãã¡ã¢ãã¦ããã¾ãã
http://code.google.com/intl/ja/apis/protocolbuffers/docs/javatutorial.html
ãã¼ã¹ãå§ç¸®å¾ã®ãã¼ã¿é·ã«ã¤ãã¦ã¯ãJavaçã®MessagePackã®ã©ã¤ãã©ãªãä½ã£ã¦ããæ¹ã以ä¸ã®ããã°ã«æ²è¼ããã¦ãã¾ãã
http://d.hatena.ne.jp/viver/20100115/p1
èªåã§ã測ã£ã¦ã¿ãªãã¨ã§ãããé常ã«åèã«ãªãã¾ããã
以ä¸ãProtocol Buffers使ãæ¹ã¡ã¢ã§ãã
protocol bufferséçºç°å¢ã®ãã¦ã³ãã¼ã
ãµã³ãã«ãå«ãã½ã¼ã¹ã³ã¼ãã®ãã¦ã³ãã¼ããã¼ã¸ã¯ä»¥ä¸ã®URLã§ãã
http://code.google.com/p/protobuf/downloads/list
2011å¹´7æ13æ¥ç¾å¨ã®ãã¼ã¸ã§ã³ã¯2.4.1ã§ãã
ãã®è¨äºã¯å°ãåã«è©¦ãããããprotobuf-java-2.4.0a.jarã使ã£ã¦ãã¾ãã
.protoã®æ¸ãæ¹
以ä¸ã¯ãã¥ã¼ããªã¢ã«ã®Person.protoã§ãã
Javaã©ã¤ã¯ãªè¨è¿°æ¹æ³ã§ãã¼ã¿æ§é ãå®ç¾©ãã¾ãã
package tutorial; option java_package = "com.example.tutorial"; //ãã®è¨è¿°ã¯ç¡ãã¦ãJavaã®å ´åã¯Javaã®ããã±ã¼ã¸ã¨ãã¦æ±ããã option java_outer_classname = "AddressBookProtos"; message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; } message AddressBook { repeated Person person = 1; }
ã³ã³ãã¤ã«
ã³ã³ãã¤ã©protocã®ã¤ã³ã¹ãã¼ã«
$ protoc -version libprotoc 2.3.0
Ubuntu 10.10ã®å ´åãã§ã«ã¤ã³ã¹ãã¼ã«ããã¦ããã¢ã¸ã¥ã¼ã«ãããã®ã§ä¸æ¦åé¤ãã¾ãã
$ sudo apt-get remove libprotobuf6
c++ã³ã³ãã¤ã©ãå¿ è¦ãªã®ã§gccã¨g++ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ sudo apt-get install gcc g++
解åãããã£ã¬ã¯ããªã«ç§»åãã¦ä»¥ä¸ãå®è¡
$ cd protobuf-2.4.0a $ ./configure $ make $ make check $ sudo make install
ã¤ã³ã¹ãã¼ã«ã®ç¢ºèª
$ export LD_LIBRARY_PATH=/usr/local/lib âUbuntuã®å ´åã¯å¿ è¦ã ã£ã $ /usr/local/bin/protoc --version libprotoc 2.4.0
ãã¨ããã¹ãéããªãã£ãã®ã§./configure --prefix=/usrãæå®ããã¹ãã§ããã
ã³ã³ãã¤ã©ã®å®è¡
$ cd tutorial $ protoc -I=tutorial --java_out=tutorial tutorial/addressbook.proto
tutorialãã£ã¬ã¯ããªã«
com/example/tutorial/AddressBookProtos.javaãçæããã¾ããã
ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³
Javaç¨ã©ã¤ãã©ãªã®ãã«ã
ãã«ãã«ã¯javaã¨mavenãå¿ è¦ã§ãã
$ sudo apt-get install openjdk-6-jdk $ sudo apt-get install maven2
ã¾ããprotocãã³ã³ãã¤ã«æ¸ã¿ã§ã¢ã«å¿ è¦ãããã¾ãã
$ mvn package
targetã«protobuf-java-2.4.0a.jarãã§ãã¾ãã
ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ã®å®è¡
$ javac -classpath .:../java/target/protobuf-java-2.4.0a.jar AddPerson.java
$ java -classpath .:../java/target/protobuf-java-2.4.0a.jar AddPerson addressbook_file addressbook_file: File not found. Creating a new file. Enter person ID: 1 Enter name: Hiroyuki Enter email address (blank for none): hrendoh@gmail.com Enter a phone number (or leave blank to finish): 090xxxxxxxx Is this a mobile, home, or work phone? mobile Enter a phone number (or leave blank to finish):