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

    }

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM