看下定義,繼承了WritableComparable接口.這個說明什么呢?
public class LongWritable
extends Object
implements org.apache.hadoop.io.WritableComparable<LongWritable>
就一個 value
private long value;
然后get set方法,常規操作
/** Set the value of this LongWritable. */public void set(long value) { this.value = value; }/** Return the value of this LongWritable. */public long get() { return value; }
hadoop2.0的組件。比較簡單
public LongWritable() {}public LongWritable(long value) { set(value); }
public interface WritableComparable<T> extends Writable, Comparable<T> {
}
繼續追根溯源. Writable里面有兩個方法,一個序列化,一個反序列化.那么為何要序列化以及反序列化呢?
public interface Writable {/** * Serialize the fields of this object to <code>out</code>.* * @param out <code>DataOuput</code> to serialize this object into.* @throws IOException*/void write(DataOutput out) throws IOException;/** * Deserialize the fields of this object from <code>in</code>. * * <p>For efficiency, implementations should attempt to re-use storage in the * existing object where possible.</p>* * @param in <code>DataInput</code> to deseriablize this object from.* @throws IOException*/void readFields(DataInput in) throws IOException;
}再看看Comparable接口
```sql
public interface Comparable<T> {/*** Compares this object with the specified object for order. Returns a* negative integer, zero, or a positive integer as this object is less* than, equal to, or greater than the specified object.** <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This* implies that <tt>x.compareTo(y)</tt> must throw an exception iff* <tt>y.compareTo(x)</tt> throws an exception.)** <p>The implementor must also ensure that the relation is transitive:* <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies* <tt>x.compareTo(z)>0</tt>.** <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for* all <tt>z</tt>.** <p>It is strongly recommended, but <i>not</i> strictly required that* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any* class that implements the <tt>Comparable</tt> interface and violates* this condition should clearly indicate this fact. The recommended* language is "Note: this class has a natural ordering that is* inconsistent with equals."** <p>In the foregoing description, the notation* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,* <tt>0</tt>, or <tt>1</tt> according to whether the value of* <i>expression</i> is negative, zero or positive.** @param o the object to be compared.* @return a negative integer, zero, or a positive integer as this object* is less than, equal to, or greater than the specified object.** @throws NullPointerException if the specified object is null* @throws ClassCastException if the specified object's type prevents it* from being compared to this object.*/public int compareTo(T o);
}
總結下來,就是要定義一個hadoop的類型,需要實現write,readFields,以及compareTo方法.
LongWritable是如何實現這幾個方法的呢?
/** Compares two LongWritables. */@Overridepublic int compareTo(LongWritable o) {long thisValue = this.value;long thatValue = o.value;return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));}
@Overridepublic void readFields(DataInput in) throws IOException {value = in.readLong();}@Overridepublic void write(DataOutput out) throws IOException {out.writeLong(value);}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态