hadoop longwritable类



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; }



  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;

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)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
     * <tt>x.compareTo(z)&gt;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);



/** Compares two LongWritables. */
  public int compareTo(LongWritable o) {
    long thisValue = this.value;
    long thatValue = o.value;
    return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
  public void readFields(DataInput in) throws IOException {
    value = in.readLong();

  public void write(DataOutput out) throws IOException {
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页