hadoop将对象串行化

关键部分
  1. 必须实现Writable接口(序列化和反序列化)。
  2. 在实现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);}
  1. Writable包装类
    包装类不包含char类型,因为可以通过IntWritable类来处理。且所有包装类都有get()和set()方法。
hadoop中标准的串行化类示例
  1. 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);
	}
}
  1. 测试类
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());
	}
}
  1. 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 ”之前进行初始化方法的执行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值