hadoop2.0的組件,hadoop longwritable類

 2023-12-06 阅读 21 评论 0

摘要:看下定義,繼承了WritableComparable接口.這個說明什么呢? public class LongWritable extends Object implements org.apache.hadoop.io.WritableComparable<LongWritable> 屬性 就一個 value private long value; 然后get set方法,常規操作 /** Set the value of this

在這里插入圖片描述

看下定義,繼承了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); }

WritableComparable接口

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

總結下來,就是要定義一個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);}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/189383.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息