IntWritable详解

来源:互联网 发布:淘宝发布宝贝 压缩包 编辑:程序博客网 时间:2024/05/18 03:35

1.Hadoop数据类型如下图:



      由上图的Writable层次结构图可以看到绝大多数的数据类型都实现了Writable、WritableComparable接口,在此先分析一下这两个接口情况。自顶下下逐步分析。

Writable接口的定义如下:

[java] view plain copy
  1. <span style="font-family:SimSun;font-size:14px;">package org.apache.hadoop.io;  
  2.   
  3. import java.io.DataOutput;  
  4. import java.io.DataInput;  
  5. import java.io.IOException;  
  6. public interface Writable {  
  7. /* 
  8.     object将自身字段序列化后的的字节流写入输出流out中。 
  9. 参数: 
  10.     out - 接收object序列化后的字节流的输出流. 
  11. */  
  12.   void write(DataOutput out) throws IOException;  
  13.     
  14.   /* 
  15.       将输入流in中的字节流反序列化然后写入object的字段 
  16.   参数: 
  17.       字节流的出处 
  18.   */  
  19.     void readFields(DataInput in) throws IOException;  
  20. }</span>  
      而DataInput、DataOutput是java.io.*中最基本的输入输出流接口,以此writable与流联系起来,其他输入输出流都需要实现DataInput与DataOutput这两个接口的方法。关于这两个接口,另外开篇分析解读。

WritableComparable接口定义如下:

[java] view plain copy
  1. <span style="font-family:SimSun;font-size:14px;">package org.apache.hadoop.io;  
  2. public interface WritableComparable<T> extends Writable, comparable<T> {  
  3. }</span>  
咋一看这个WritableComparable没有方法,其实它的方法全都是通过继承而来的,Writable接口上面已经分析了,所以WritableComparable以下两个方法。

[java] view plain copy
  1. <span style="font-family:SimSun;font-size:14px;">void write(DataOutput out) throws IOException;  
  2.   
  3. void readFields(DataInput in) throws IOException;</span>  

还有来自comparable的方法,comparable是属于java.lang.*中的一个接口,它只有一个方法。

[java] view plain copy
  1. <span style="font-family:SimSun;font-size:14px;">int compareTo( T other);  
  2. /* 
  3.     比较此对象与指定对象other的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。 
  4.  
  5.     参数:o - 要比较的对象。 
  6.  
  7.     返回:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。  
  8. */</span>  

2.IntWritable类定义如下:

[java] view plain copy
  1. <span style="font-family:SimSun;font-size:14px;">package org.apache.hadoop.io;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** A WritableComparable for ints. */  
  6. public class IntWritable implements WritableComparable {  
  7.   private int value;  
  8.   
  9.   public IntWritable() {}  
  10.   
  11.   public IntWritable(int value) { set(value); }  
  12.   
  13.   /** Set the value of this IntWritable. */  
  14.   public void set(int value) { this.value = value; }  
  15.   
  16.   /** Return the value of this IntWritable. */  
  17.   public int get() { return value; }  
  18.   
  19.   public void readFields(DataInput in) throws IOException {  
  20.     value = in.readInt();  
  21.   }  
  22.   
  23.   public void write(DataOutput out) throws IOException {  
  24.     out.writeInt(value);  
  25.   }  
  26.   
  27.   /** Returns true iff o is a IntWritable with the same value. */  
  28.   public boolean equals(Object o) {  
  29.     if (!(o instanceof IntWritable))  
  30.       return false;  
  31.     IntWritable other = (IntWritable)o;  
  32.     return this.value == other.value;  
  33.   }  
  34.   
  35.   public int hashCode() {  
  36.     return value;  
  37.   }  
  38.   
  39.   /** Compares two IntWritables. */  
  40.   public int compareTo(Object o) {  
  41.     int thisValue = this.value;  
  42.     int thatValue = ((IntWritable)o).value;  
  43.     return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));  
  44.   }  
  45.   
  46.   public String toString() {  
  47.     return Integer.toString(value);  
  48.   }  
  49.   
  50.   /** A Comparator optimized for IntWritable. */   
  51.   public static class Comparator extends WritableComparator {  
  52.     public Comparator() {  
  53.       super(IntWritable.class);  
  54.     }  
  55.   
  56.     public int compare(byte[] b1, int s1, int l1,  
  57.                        byte[] b2, int s2, int l2) {  
  58.       int thisValue = readInt(b1, s1);  
  59.       int thatValue = readInt(b2, s2);  
  60.       return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));  
  61.     }  
  62.   }  
  63.   
  64.   static {                                        // register this comparator  
  65.     WritableComparator.define(IntWritable.classnew Comparator());  
  66.   }  
  67. }</span>  

3.一般对于自定义数据类型要实现Writable接口,因为数据在网络传输或者进行永久性存储的时候,需要序列化和反序列化。如果该数据类型要作为主键使用或者要进行比较大小的操作,还要实现WritableComparable接口。

如:

[java] view plain copy
  1. <span style="font-family:SimSun;font-size:14px;">public class Point3D implements WritableComparable<Point3D>  
  2.   
  3. {  
  4.   
  5.     private float x,y,z;  
  6.   
  7.     public float getX(){return x;}  
  8.   
  9.     public float getY(){return y;}  
  10.   
  11.     public float getZ(){return z;}  
  12.   
  13.     public void readFields(DataInput in) throws IOException  
  14.   
  15.     {  
  16.   
  17.         x = in.readFloat();  
  18.   
  19.         y = in.readFloat();  
  20.   
  21.         z = in.readFloat();  
  22.   
  23.     }  
  24.   
  25.     public void write(DataOutput out) throws IOException  
  26.   
  27.     {  
  28.   
  29.          out.writeFloat(x);  
  30.   
  31.          out.writeFloat(y);  
  32.   
  33.          out.writeFloat(z);  
  34.   
  35.     }  
  36.   
  37.    
  38.   
  39.     public int CompareTo(Point3D p)  
  40.   
  41.     {  
  42.   
  43.         //具体实现比较当前的空间坐标点this(x,y,z)与指定的点p(x,y,z)的大小  
  44.   
  45.         // 并输出: -1(小于), 0(等于), 1(大于)  
  46.   
  47.     }  
  48.   
  49. }</span>  

http://www.cnblogs.com/zhengyuhong/p/3952954.html

原创粉丝点击