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.