Hadoop detailed (12) custom writable

Source: Internet
Author: User
Tags split tostring

Custom writable

Hadoop has implemented some very useful writable, and you can do a lot of things with their combinations, but if you want to construct some more complex results, you can customize writable to achieve your goal, we annotate the way Explain the custom writable (not to mention that I only post the code for space Oh, posture in the note):

Package com.sweetop.styhadoop;  
Import Org.apache.hadoop.io.Text;  
      
Import org.apache.hadoop.io.WritableComparable;  
Import Java.io.DataInput;  
Import Java.io.DataOutput;  
      
Import java.io.IOException; 
 /** * Created with IntelliJ idea. * User:lastsweetop * date:13-7-17 * Time: PM 8:50 * To change this template use File | Settings | 
 File Templates.  
    * * Public class Emploeewritable implements writablecomparable<emploeewritable>{private Text name;  
      
    Private Text role;  
        /** * Must have a default construction vessel so that the MapReduce method can create an object and then read the assignment from the serialized data stream via the ReadFields method/public emploeewritable () {  
    Set (new text (), new text ());  
    Public emploeewritable (text name, text role) {set (Name,role);  
        public void Set (Text name,text role) {this.name = name;  
    This.role = role;  
    Public Text GetName () {return name; } pUblic Text Getrole () {return role; 
    /** * Serializes each member object into the output stream through the Write method of the member object itself * @param dataoutput * @throws IOException * *  
        @Override public void Write (DataOutput dataoutput) throws IOException {name.write (dataoutput);  
    Role.write (DataOutput); 
     /** * Ditto the ReadFields method that invokes the member object itself, deserializing each member object from the input stream * @param datainput * @throws IOException */@Override public void ReadFields (Datainput datainput) throws IOException {Name.readfields (Datai  
        Nput);  
    Role.readfields (Datainput); 
     /** * Implements writablecomparable a method that must be implemented to compare sorting * @param emploeewritable * @return */@Override public int compareTo (emploeewritable emploeewritable) {int cmp = Name.compareto (Emplo  
        Eewritable.name);  
        if (cmp!=0) {return CMP; Return Role.compareto (Emploeewritable.role);  
    }/** * MapReduce requires a split (partitioner) to divide the map output into chunks by feeding multiple reduce) * The default is Hashpatitioner, he is 
     The Hashcode function of the object is segmented, so the good or bad of the hashcode determines whether the split is even, he is a very critical method.  
    * @return */@Override public int hashcode () {return Name.hashcode () *163+role.hashcode (); @Override public boolean equals (Object o) {if (o instanceof emploeewritable) {E  
            Mploeewritable emploeewritable= (emploeewritable) o;  
        Return Name.equals (emploeewritable.name) && role.equals (emploeewritable.role);  
    return false; /** * If you want to customize the output of Textoutputformat as output format, you need to rewrite the tostring method * @return * * * @Override PU  
    Blic String toString () {return name+ "\ t" +role; }  
}

Writable objects are subject to change and are often reused, so try to avoid assigning objects in write and ReadFields.

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.