Stepping on the footprints of predecessors to learn hadoop--serialization, writerable

Source: Internet
Author: User

Package Org.apache.hadoop.io;

Import Java.io.DataOutput;
Import Java.io.DataInput;
Import java.io.IOException;

/**
* A serializable object which implements a simple, efficient,

A serialized object, this guy implements a simple, efficient, serialized protocol that is based on the two IO objects of the datainput and DataOutput
* protocol, based on {@link datainput} and {@link dataoutput}.
*
* <p>any <code>key</code> or <code>value</code> type in the Hadoop map-reduce

Any data based on the Key-value type in the Hadoop map-reduce framework is an implementation of this interface
* Framework implements this interface.</p>
*
* <p>implementations typically implement a static <code>read (datainput) </code>
* Method which constructs a new instance, calls {@link #readFields (datainput)}
* and returns the instance.</p>
*
* <p>Example:</p>
* <p><blockquote><pre>
* Public class Mywritable implements writable {
*//Some data
* Private int counter;
* Private long timestamp;
*
* public void Write (DataOutput out) throws IOException {
* Out.writeint (counter);
* Out.writelong (timestamp);
*       }
*
* public void ReadFields (Datainput in) throws IOException {
* counter = In.readint ();
* timestamp = In.readlong ();
*       }
*
* public static mywritable read (Datainput in) throws IOException {
* Mywritable w = new mywritable ();
* W.readfields (in);
* Return w;
*       }
*     }
* </pre></blockquote></p>
*/
Public interface Writable {
/**
* Serialize The fields of this object to <code>out</code>.
*
* @param out <code>DataOuput</code> to serialize this object into.
* @throws IOException
*/
void Write (DataOutput out) throws IOException;

/**
* Deserialize the fields of this object from <code>in</code>.
*
* <p>for efficiency, implementations should attempt to re-use storage in the
* Existing object where possible.</p>
*
* @param in <code>DataInput</code> to deseriablize the object from.
* @throws IOException
*/
void ReadFields (Datainput in) throws IOException;
}

--------------------------------------------------------------------------------------------------------------- --------------------

int
readInt()
Reads four input bytes and returns a int value.

long
readLong()
Reads eight input bytes and returns a long value.

--------------------------------------------------------------------------------------------------------------- --------------------

void
writeInt(int v)
Writes a int value to the output stream, which consists of four bytes.

void
writeLong(long v)
Writes a long value to the output stream, which consists of eight bytes.

--------------------------------------------------------------------------------------------------------------- --------------------------


/** A polymorphic Writable that writes an instance with it's class name.
* Handles arrays, strings and primitive types without a writable wrapper.
*/
Public class Objectwritable implements writable, configurable {

Private Class Declaredclass;
Private Object instance;
Private Configuration conf;

--------------------------------------------------------------------------------------------------------------- ----------------------------

Writable writable = Writablefactories.newinstance (Instanceclass, conf);
Writable.readfields (in);
instance = writable;

Stepping on the footprints of predecessors to learn hadoop--serialization, writerable

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.