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());
	}
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值