beebe_3 发表于 2016-12-12 09:56:58

Hadoop 中数据的序列化与反序列化

  1 作用:
  序列化和反序列化就是结构化对象和字节流之间的转换,主要用在内部进程的通讯和持久化存储方面
  2 个人理解:因为hadoop 网络传输和本地文件保存比较多,序列化的数据更加方便的进行数据通信和对象的持久化
  3 实现接口WritableableComparable 并且实现序列化与反序列化的方法 ,注意写入参数和读出的参数顺序一直就可以了 (write和readField方法)
  4 重写toString方法,为了流输出的时候使用
   1 )序列化的对象

public class Student implements WritableComparable {

private Text name = new Text();
private IntWritable age = new IntWritable();
private Text sex = new Text();
public Student() {
}
public Student(String name, int age, String sex) {
super();
this.name = new Text(name);
this.age = new IntWritable(age);
this.sex = new Text(sex);
}
//set 和get方法省略
public void readFields(DataInput in) throws IOException {
name.readFields(in);
age.readFields(in);
sex.readFields(in);
}
public void write(DataOutput out) throws IOException {
name.write(out);
age.write(out);
sex.write(out);
}
public int compareTo(Object o) {
Student s = (Student) o;
int result = 0;
if ((result = name.compareTo(s.getName())) != 0)
return result;
if ((result = age.compareTo(s.getAge())) != 0)
return result;
if ((result = sex.compareTo(s.getSex())) != 0)
return result;
return 0;
}
}

   
  2 序列化对象的使用(对象写到文件中和从文件中直接读取对象)

public class Client {
public static void main(String[] args) throws IOException {
Student s = new Student("123", 20, "网站");// 从此开始序列化
FileOutputStream fout = new FileOutputStream(new File(
"F:\\testWritable.txt"));
DataOutputStream out = new DataOutputStream(fout);
s.write(out);
fout.close();
out.close();
Student s1 = new Student(); // 从此开始是反序列化
FileInputStream fin = new FileInputStream(new File(
"F:\\testWritable.txt"));
DataInputStream in = new DataInputStream(fin);
s1.readFields(in);
System.out.println("name = " + s1.getName() + ",age = " + s1.getAge()
+ ",sex =" + s1.getSex());
}
}

   
页: [1]
查看完整版本: Hadoop 中数据的序列化与反序列化