Hadoop2.7.3 Writable 序列化之一
1. Hadoop序列化背景或原因
- Java序列化会附带额外的信息,浪费带宽资源
- Java反序列化,会不断创建新的对象,无法实现对象共享
- Hadoop处理大文件的特征决定
- 大数据平台序列化特征:
(1)紧凑:序列化数据,无须携带太多附加信息,减少带宽压力
(2)快速:进程间通信需要高效,需减少序列化和反序列化的开销;
(3)可扩展:系统、协议升级,序列化随着升级
(4)互操作:需支持不同语言间的通信
2. Hadoop Writable机制
- 在Hadoop中需要实现Writable接口来实现序列化,这个是Hadoop默认的一种序列化方式
3. 实验内容
3.1 思路
思路:借助Java序列化的思路来完成Hadoop序列化测试
1、创建实现类,实现Writable接口
2、在实现类中定义成员变量(成员变量的类型都是Java的数据类型)
3、设置get/set方法、无参构造方法、有参构造方法
4、重写toString方法
5、编写测试类
3.2 编写HadoopPerson类
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class HadoopPerson implements Writable {
private int id;
private String name;
private int age;
private String phone;
public HadoopPerson() {
this.id = 0;
this.name = "";
this.age = 0;
this.phone = "";
}
public HadoopPerson(int id, String name, int age, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
}
public void write(DataOutput out) throws IOException {
out.writeInt(this.id);
out.writeUTF(this.name);
out.writeInt(this.age);
out.writeUTF(this.phone);
}
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = in.readUTF();
this.age = in.readInt();
this.phone = in.readUTF();
}
@Override
public String toString() {
return "HadoopPerson{" +
"id=" + id +
", name=" + name +
", age=" + age +
", phone=" + phone +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3.3 编写测试类
import java.io.*;
public class TestMain {
public static void main(String[] args) throws Exception {
ser();
deSer();
}
public static void ser() throws Exception{
File file = new File("F:\\person.txt");
FileOutputStream outputStream = new FileOutputStream(file);
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
HadoopPerson person = new HadoopPerson(1001,"苏江明",30,"18275128972");
person.write(dataOutputStream);
dataOutputStream.close();
outputStream.close();
System.out.println("序列化成功~");
}
public static void deSer() throws Exception{
File file = new File("F:\\person.txt");
FileInputStream inputStream = new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(inputStream);
HadoopPerson person = new HadoopPerson();
person.readFields(dataInputStream);
dataInputStream.close();
inputStream.close();
System.out.println("反序列化成功~" + person);
}
}
4. 实验结果
4.1 调用ser()序列化后文件内容如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/25f8e9076a97bed8df6217c32b67fcce.png)
4.1 调用反序列化方法deSer()结果如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fd34c64c1f66a764e9abd80c90556a51.png)