Custom writable Class

Source: Internet
Author: User
Tags comparable

Take intwritable as an example to introduce the steps to customize the writable

//the Writablecomparable interface inherits the writable interface and the comparable interface Public classIntwritableImplementsWritablecomparable<intwritable> {//define member variables for ordinary Java typesPrivate intvalue;//set method for member variables Public voidSetintValue) { This. Value =value;}//get methods for member variables Public intGet () {returnvalue;} //Non-parametric construction method, called by the Mr Frame reflection mechanism Publicintwritable () {}//a method for constructing a parameter PublicIntwritable (intvalue) {Set (value);}//Deserialization Method Public voidReadFields (Datainput in)throwsIOException {Value=in.readint ();}//Serialization Methods Public voidWrite (DataOutput out)throwsIOException {out.writeint (value);}//overwrite Equals () method Public Booleanequals (Object o) {if(! (Oinstanceofintwritable)) return false; Intwritable Other=(intwritable) o; return  This. Value = =Other.value;}//overwrite Hashcode () method Public inthashcode () {returnvalue;}//Overwrite ToString () method PublicString toString () {returninteger.tostring (value);}//overwrite the CompareTo () method in the comparable interface "default Ascending" Public intcompareTo (intwritable o) {intThisvalue = This. Value; intThatvalue =O.value; return(Thisvalue<thatvalue-1: (Thisvalue==thatvalue 0:1)));} //1. Define inner class comparator "comparer" inherits from Writablecomparator class Public Static classComparatorextendsWritablecomparator {//2. An indispensable parameterless constructor, which is called by the reflection mechanism   PublicComparator () {Super(intwritable.class); }    //3. The comparison sort of the write throttling level   Public intComparebyte[] B1,intS1,intL1,byte[] B2,intS2,intL2) {    //returns the encoded value of the character array B1    intThisvalue =ReadInt (B1, S1); intThatvalue =readInt (B2, S2); return(Thisvalue<thatvalue-1: (Thisvalue==thatvalue 0:1))); }}//Registering a custom writable class with the Writablecomparator class "haoop automatically calls the comparator above"Static{writablecomparator.define (intwritable).class,NewComparator ());}}
View Code

1) When implementing a byte-stream comparison in a custom writable class, the Rawcomparator class is not inherited directly, but inherits its subclass Writablecomparator, because subclasses provide us with useful tool methods such as reading int from a byte array, Long and Vlong equivalents. Overwrite public int compare (byte[] b1, int s1, int L1, byte[] b2, int s2, int L2) method.

2) Of course, after writing the Compare () method, do not forget the Rawcomparator class that was written for the custom writable class registration.

3) for the specific implementation of the Readint () tool method in the code:

/***  /publicstaticint readInt (byteint start) {    return ((Bytes[start  & 0xff) <<) +            ((Bytes[start+1] & 0xFF) << +            ((Bytes[start+2] & 0xff) <<  8) +            ((Bytes[start+3) & 0xff));  }
View Code

Custom writable Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.