Hadoop广泛用到Protobuf,而且Protobuf的效率极高,网络间传输数据量小。
首先需要新建一个描述对象,在这里我们明明命名为addressbook.proto
package tutorial;
option java_package = "com.example.tutorial";
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;
}
然后对它执行protobuf的编译命令
./protoc --java_out=. addressbook.proto
然后将生成的java文件导入到IED中,就可以编程了。
也新建了一个java自带的序列化
package com.example.tutorial;
import java.io.Serializable;
public class JavaSerializableBean implements Serializable {
private String name;
private String PhoneNo;
private int id;
private String email;
public JavaSerializableBean(){
}
public JavaSerializableBean(String name, String phoneNo, int id, String email) {
this.name = name;
PhoneNo = phoneNo;
this.id = id;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNo() {
return PhoneNo;
}
public void setPhoneNo(String phoneNo) {
PhoneNo = phoneNo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
import com.example.tutorial.AddressBookProtos;
import com.example.tutorial.JavaSerializableBean;
import org.junit.Test;
import java.io.*;
public class ProtobufTest {
/**
* 将protobuf序列化后的文件写入磁盘
* @throws Exception
*/
@Test
public void write() throws Exception {
AddressBookProtos.Person tom=AddressBookProtos.Person.newBuilder()
.setId(12345)
.setName("tom")
.setEmail("123@123.123")
.addPhone(AddressBookProtos.Person.PhoneNumber.newBuilder()
.setNumber("+351 999 999 999")
.setType(AddressBookProtos.Person.PhoneType.HOME)
.build())
.build();
tom.writeTo(new FileOutputStream("/Users/simmucheng/tmp/protobuf_test/persion.data"));
}
/**
* 将写入磁盘的序列化文件反序列化读取出来
* @throws Exception
*/
@Test
public void read() throws Exception {
AddressBookProtos.Person tom=AddressBookProtos.Person.parseFrom(
new FileInputStream("/Users/simmucheng/tmp/protobuf_test/persion.data"));
System.out.println(tom.getName());
}
@Test
public void SerializableWrite() throws Exception {
JavaSerializableBean bean=new JavaSerializableBean();
bean.setEmail("123@123.123");
bean.setName("tom");
bean.setId(12345);
bean.setPhoneNo("+351 999 999 999");
ObjectOutputStream obj=new ObjectOutputStream(
new FileOutputStream("/Users/simmucheng/tmp/protobuf_test/bean.data"));
obj.writeObject(bean);
obj.close();
}
@Test
public void SerializableRead() throws Exception {
ObjectInputStream obj=new ObjectInputStream(
new FileInputStream("/Users/simmucheng/tmp/protobuf_test/bean.data"));
JavaSerializableBean bean= (JavaSerializableBean) obj.readObject();
System.out.println(bean.getName());
}
}
下面放一个Protobuf的上手文档
https://github.com/xiammu/BookNote/blob/master/Google%20Protocol%20Buffers.pdf