Hadoop 序列化对象(序列化和反序列化)
2013-12-07 21:32
330 查看
简单讲解如何序列化和反序列化。啥都不说了,直接粘代码:
package com.test; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparable; public class PersonWritable implements WritableComparable<PersonWritable> { Text name = new Text(); Text sex = new Text(); IntWritable age = new IntWritable(); public PersonWritable() { set("tom", "man", 12); } public void set(String name, String sex, int age) { this.name = new Text(name); this.sex = new Text(sex); this.age = new IntWritable(age); } public PersonWritable(String name, String sex, int age) { set(name, sex, age); } @Override public String toString() { return "PersonWritable [name=" + name.toString() + ", sex=" + sex.toString() + ", age=" + age.get() + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((sex == null) ? 0 : sex.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; PersonWritable other = (PersonWritable) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex == null) { if (other.sex != null) return false; } else if (!sex.equals(other.sex)) return false; return true; } @Override public void readFields(DataInput arg0) throws IOException { name.readFields(arg0); sex.readFields(arg0); age.readFields(arg0); } @Override public void write(DataOutput arg0) throws IOException { name.write(arg0); sex.write(arg0); age.write(arg0); } @Override public int compareTo(PersonWritable o) { int result = name.compareTo(o.name); if (result != 0) { return result; } int result1 = sex.compareTo(o.sex); if (result1 != 0) { return result1; } int result2 = age.compareTo(o.age); if (result2 != 0) { return result2; } return result2; } }
package com.test.myselfwritable; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import org.apache.hadoop.io.Writable; public class HadoopSerializationUtil { public static byte[] serialize(Writable writable) throws IOException { // create bytes ByteArrayOutputStream ByteArrayOutputStream out = new ByteArrayOutputStream(); // create DataOutputStream DataOutputStream dataout = new DataOutputStream(out); // call write method writable.write(dataout); dataout.close(); // bytes return out.toByteArray(); } public static void deserialize(Writable writable, byte[] bytes) throws Exception { // create ByteArrayInputStream ByteArrayInputStream in = new ByteArrayInputStream(bytes); // create DataInputStream DataInputStream datain = new DataInputStream(in); // read fields writable.readFields(datain); datain.close(); } }测试的类:
package com.test; import org.apache.hadoop.util.StringUtils; import com.test.myselfwritable.HadoopSerializationUtil; public class Test { public static void main(String[] args) throws Exception { // test serilizable System.out.println("test1"); PersonWritable personWritable = new PersonWritable("tom", "man", 13); // begin serialztion byte[] result = HadoopSerializationUtil.serialize(personWritable); System.out.print(StringUtils.byteToHexString(result)); System.out.println("test2"); PersonWritable personWritable1 = new PersonWritable(); HadoopSerializationUtil.deserialize(personWritable1, result); System.out.print(personWritable1.toString()); } }
相关文章推荐
- Hadoop学习--person类对象的序列化和反序列化--day07
- vc++上的MFC的对象序列化和反序列化
- 对象序列化及反序列化
- objective-c自定义类对象的序列化和反序列化
- [Java] 对象的序列化和反序列化
- java序列化是什么和反序列化和hadoop序列化
- vc++上的MFC的对象序列化和反序列化
- 将java对象存储到redis数据库(使用序列化和反序列化)
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- XmlSerializer 对象的Xml序列化和反序列化
- XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置
- 【ASP.NET】DataContract序列化,反序列化对象中包含用接口声明的属性时的处理方法
- .NET对象的XML序列化和反序列化(转)
- C#中XML与对象之间的序列化、反序列化
- java对象的序列化和反序列化
- Java对象以Blob存储,及序列化反序列化 (1)
- 把对象序列化为xml格式和反序列化
- Java对象的序列化和反序列化实践
- C#实现对象的Xml格式序列化及反序列化
- C#实现对象的Xml格式序列化及反序列化