標籤:style blog java color os 資料
在看hadoop 的二次排序的時候,改寫了下, 加了第三個參數, 本來以為是在
public int compareTo(IntPair o) { System.out.println("-----------compareTo"); if (first != o.first) { return first < o.first ? -1 : 1; } else if (second != o.second) { return second < o.second ? -1 : 1; }else if (third != o.third) { return third < o.third ? -1 : 1;} return 0; }
本來以為排序在這裡面進行, 後來發現不是,把比較第3個欄位的代碼去掉, 發現還是有序的。
後來通過列印得知在compare函數中,稍微改寫了下
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { // 位元組讀取 int intvalue = readInt(b1, s1); System.out.println("s1 = " + b1.length);
// 驗證b1中儲存的資料 int third = 0; for(int i =s1 + 9; i<= s1+ 12; i++){ third += (b1[i]&0xff) << (24-8*i); } System.out.println("third = " + third); return compareBytes(b1, s1, l1, b2, s2, l2); } }
有3個整形值, s1為開始位置, l1為長度12, 這樣我們就可以讀出我們的值
return compareBytes(b1, s1, l1, b2, s2, l2);調用 return FastByteComparisons.compareTo(b1, s1, l1, b2, s2, l2);
public int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if ((buffer1 == buffer2) && (offset1 == offset2) && (length1 == length2)) { return 0; } int end1 = offset1 + length1; int end2 = offset2 + length2; int i = offset1; for (int j = offset2; (i < end1) && (j < end2); ++j) { int a = buffer1[i] & 0xFF; int b = buffer2[j] & 0xFF; if (a != b) return (a - b); ++i; } return (length1 - length2); } } }
從代碼中就知道了,通過位元組數組比較三個值, 這樣就出來的結果就是有序的了
結論, 理論上N個欄位這樣出來的結果的都是有序的,只是比較的長度有所變化
測試又加了一個欄位, 輸出結果都是有序的。
測試代碼
public static class IntPair implements WritableComparable<IntPair> { private int first = 0; private int second = 0; private int third = 0; private int fourth = 0; /** * Set the left and right values. */ public void set(int left, int right, int third, int fourth) { first = left; second = right; this.third = third; this.fourth = fourth; } public int getFirst() { return first; } public int getSecond() { return second; } public int getThird() { return third; } public int getFourth() { return fourth; } @Overridepublic String toString() { System.out.println("third = " + third); return first + "\t" + second + "\t" + third + "\t" + fourth;}/** * Read the two integers. * Encoded as: MIN_VALUE -> 0, 0 -> -MIN_VALUE, MAX_VALUE-> -1 */ @Override public void readFields(DataInput in) throws IOException { first = in.readInt();// + Integer.MIN_VALUE; second = in.readInt();// + Integer.MIN_VALUE; third = in.readInt();// + Integer.MIN_VALUE; fourth = in.readInt(); } @Override public void write(DataOutput out) throws IOException { /* out.writeInt(first - Integer.MIN_VALUE); out.writeInt(second - Integer.MIN_VALUE); out.writeInt(third - Integer.MIN_VALUE); */ out.writeInt(first ); out.writeInt(second ); out.writeInt(third ); out.writeInt(fourth); } @Override public int hashCode() { return first * 157 + second*10 + third; } @Override public boolean equals(Object right) { if (right instanceof IntPair) { IntPair r = (IntPair) right; return r.first == first && r.second == second && r.third == third && r.fourth == fourth; } else { return false; } } /** A Comparator that compares serialized IntPair. */ public static class Comparator extends WritableComparator { public Comparator() { super(IntPair.class); } // 排序比較子,資料全部存在byte數組 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { // 位元組讀取 int intvalue = readInt(b1, s1); System.out.println("s1 = " + b1.length); int third = 0; for(int i =s1 + 9; i<= s1+ 12; i++){ third += (b1[i]&0xff) << (24-8*i); } System.out.println("third = " + third); return compareBytes(b1, s1, l1, b2, s2, l2); } } static { // register this comparator WritableComparator.define(IntPair.class, new Comparator()); } // 好像沒用上 @Override public int compareTo(IntPair o) { System.out.println("-----------compareTo"); if (first != o.first) { return first < o.first ? -1 : 1; } else if (second != o.second) { return second < o.second ? -1 : 1; }// else if (third != o.third) { // return third < o.third ? -1 : 1;} return 0; } }