When dealing with complex business in Hadoop, it is necessary to use the composite key, which is different from the simple inherited writable interface, but inherits the Writablecomparable<t> interface, in fact, writablecomparable<t The > interface inherits the writable and Comparable<t> interfaces, and if only one class is used as a value-transfer object and not as a key, the writable interface can be inherited.
On the Source:
Public interface writablecomparable<t> extends writable, comparable<t> {}
Public interface Writable { void write (DataOutput out) throws IOException; void ReadFields (Datainput in) throws IOException;}
Public interface Comparable<t> {public int compareTo (T o);}
The following is an example of implementing a composite key, pro-test, available
public class SortKey implements Writablecomparable<sortkey>{private Text name;private intwritable right;public SortKey () {Set (new Text (), New intwritable ());} Public SortKey (Text name, intwritable right) {Set (name, right);} private void Set (Text name,intwritable right) {this.name = Name;this.right = right;} /** * @return The name */public Text getName () {return name;} /** * @param name the name to set */public void SetName (Text name) {this.name = name;} /** * @return The right */public intwritable getRight () {return to right;} /** * @param right to set */public void Setright (intwritable right) {this.right = right;} @Overridepublic void Write (DataOutput out), throws IOException {Name.write (out), Right.write (out);} @Overridepublic void ReadFields (Datainput in) throws IOException {Name.readfields (in); Right.readfields (in);} @Overridepublic int compareTo (SortKey o) {int cmp = Name.compareto (o.name); if (cmp! = 0) {return CMP;} Else{return Right.compareto (O.right);}}
<span style= "White-space:pre" ></span>//so far, you can only use it as a key, but if you need to sort by one of the key values, here's the point.
Static{writablecomparator.define (Sortkey.class, New Comparator ());} public static class Comparator extends writablecomparator{ private static final text.comparator Text_comparator = new Text.comparator ();p rotected Comparator () {super (sortkey.class);} /* (non-javadoc) * @see Org.apache.hadoop.io.writablecomparator#compare (byte[], int, int, byte[], int, int) */@ overridepublic int Compare (byte[] b1, int s1, int L1, byte[] b2, int s2, int l2) {Try{int firstL1 = Writableutils.decodevi Ntsize (B1[S1]) + Readvint (B1, s1); int firstL2 = Writableutils.decodevintsize (B2[s2]) + readvint (B2, S2); Return Text_comparator.compare (B1, S1, firstL1, B2, S2, firstL2);} catch (Exception e) {throw new IllegalArgumentException (e);}}}
How to use Hadoop composite key sorting