简单讲解如何序列化和反序列化。啥都不说了,直接粘代码:
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());
}
}