关键部分
- 必须实现Writable接口(序列化和反序列化)。
- 在实现Writable接口的过程中,要实现write()方法。在该方法中要使用hadoop基本数据类型包装类的write()方法。
public class Person implements Writable{
String name;
int age;
public void write(DataOutput out){
new Text(name).write(out);
new IntWritable(age).write(out);
}
}
- Writable包装类
包装类不包含char类型,因为可以通过IntWritable类来处理。且所有包装类都有get()和set()方法。
hadoop中标准的串行化类示例
- POJO/DTO
public class Person implements Writable{
private Text name;
private IntWritable age;
private BooleanWritable gender;
public void setName(Text name){
this.name=name;
}
public Text getName(){
return name;
}
public void setAge(IntWritable age){
this.age=age;
}
public IntWritable getAge(){
return Age;
}
public void setGender(BooleanWritable gender){
this.gender=gender;
}
public BooleanWritable getGender(){
return gender;
}
//serialization
public void write(DataOutput out) throws IOException {
name.write(out);
age.write(out);
gender.write(out);
}
//deserialization
public void readFields(DataInput in) throws IOException {
name=new Text();
age=new IntWritable();
gender=new BooleanWritable();
name.readFields(in);
age.readFields(in);
gender.readFields(in);
}
}
- 测试类
public class Test{
@Test
public void testSerial() throws Exception{
//new DTO
Person p=new Persion();
p.setName(new Text("bee"));
p.setAge(new IntWritable(18));
p.setGender(new BooleanWritable(false));
//serializing
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dataOut=new DataOutputStream(baos);
p.write(dataOut);
dataOut.close();
//deserializing
Person newPerson=new Person();
newPerson.readFields(new DataInputStream(new ByteArrayInputSream(baos.toByteArray())))
System.out.println(newPerson.getName());
System.out.println(newPerson.getAge().get());
}
}
- Map数据类型的串行化
public class Test{
@Test
public void test() throws Exception{
MapWritable map=new MapWritable();
map.put(new IntWritable(100), new Text("tom"));
map.put(new IntWritable(200), new Text("jarry"));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dataOut=new DataOutputStream(baos);
map.write(dataOut);
dataOut.close();
MapWritable map2=new MapWritable();
map2.readFields(new DataInputStream(new ByteArrayInputStream(baos.toByteArray())));
}
}
注意:
可以使用“ @BeforeClass ”注解(JUnit)实现在“ @Test ”之前进行初始化方法的执行。