二. 用户自定义数据类型的实现
1.继承接口Writable,实现其方法write()和readFields(), 以便该数据能被序列化后完成网络传输或文件输入/输出;
2.如果该数据需要作为主键key使用,或需要比较数值大小时,则需要实现WritalbeComparable接口,实现其方法write(),readFields(),CompareTo() 。
public class Point3D implements Writable<Point3D>
{
private float x,y,z;
public float getX(){return x;}
public float getY(){return y;}
public float getZ(){return z;}
public void readFields(DataInput in) throws IOException
{
x = in.readFloat();
y = in.readFloat();
z = in.readFloat();
}
public void write(DataOutput out) throws IOException
{
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(z);
}
}
public class Point3D implements WritableComparable<Point3D>
{
private float x,y,z;
public float getX(){return x;}
public float getY(){return y;}
public float getZ(){return z;}
public void readFields(DataInput in) throws IOException
{
x = in.readFloat();
y = in.readFloat();
z = in.readFloat();
}
public void write(DataOutput out) throws IOException
{
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(z);
}
public int CompareTo(Point3D p)
{
//具体实现比较当前的空间坐标点this(x,y,z)与指定的点p(x,y,z)的大小
// 并输出: -1(小于), 0(等于), 1(大于)
}
}