HADOOP-7151. Document need for stable hashCode() in WritableComparable. Contributed by Dmitriy V. Ryaboy.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1074241 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2011-02-24 18:05:46 +00:00
parent 27df75e6bb
commit 4d53649f49
2 changed files with 22 additions and 7 deletions

View File

@ -57,6 +57,9 @@ Trunk (unreleased changes)
HADOOP-6376. Add a comment header to conf/slaves that specifies the file HADOOP-6376. Add a comment header to conf/slaves that specifies the file
format. (Kay Kay via todd) format. (Kay Kay via todd)
HADOOP-7151. Document need for stable hashCode() in WritableComparable.
(Dmitriy V. Ryaboy via todd)
OPTIMIZATIONS OPTIMIZATIONS
BUG FIXES BUG FIXES

View File

@ -28,12 +28,16 @@
* via <code>Comparator</code>s. Any type which is to be used as a * via <code>Comparator</code>s. Any type which is to be used as a
* <code>key</code> in the Hadoop Map-Reduce framework should implement this * <code>key</code> in the Hadoop Map-Reduce framework should implement this
* interface.</p> * interface.</p>
*
* <p>Note that <code>hashCode()</code> is frequently used in Hadoop to partition
* keys. It's important that your implementation of hashCode() returns the same
* result across different instances of the JVM. Note also that the default
* <code>hashCode()</code> implementation in <code>Object</code> does <b>not</b>
* satisfy this property.</p>
* *
* <p>Example:</p> * <p>Example:</p>
* <p><blockquote><pre> * <p><blockquote><pre>
* public class MyWritableComparable implements * public class MyWritableComparable implements WritableComparable {
* WritableComparable&lt;MyWritableComparable&gt; {
*
* // Some data * // Some data
* private int counter; * private int counter;
* private long timestamp; * private long timestamp;
@ -48,10 +52,18 @@
* timestamp = in.readLong(); * timestamp = in.readLong();
* } * }
* *
* public int compareTo(MyWritableComparable other) { * public int compareTo(MyWritableComparable w) {
* int thisValue = this.counter; * int thisValue = this.value;
* int thatValue = other.counter; * int thatValue = ((IntWritable)o).value;
* return (thisValue &lt; thatValue ? -1 : (thisValue == thatValue ? 0 : 1)); * return (thisValue &lt; thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
* }
*
* public int hashCode() {
* final int prime = 31;
* int result = 1;
* result = prime * result + counter;
* result = prime * result + (int) (timestamp ^ (timestamp &gt;&gt;&gt; 32));
* return result
* } * }
* } * }
* </pre></blockquote></p> * </pre></blockquote></p>