首页 > 代码库 > Hadoop数据类型

Hadoop数据类型

 一.  Hadoop内置的数据类型

  • BooleanWritable:标准布尔型数值
  • ByteWritable:单字节数值
  • DoubleWritable:双字节数值
  • FloatWritable:浮点数
  • IntWritable:整型数
  • LongWritable:长整型数
  • Text:使用UTF8格式存储的文本
  • NullWritable:当<key, value>中的key或value为空时使用

二. 用户自定义数据类型的实现

     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(大于)

    }

}

 

转自:http://www.cnblogs.com/anny-1980/articles/2608097.html

Hadoop数据类型