Hadoop detailed (13) serialization framework

Source: Internet
Author: User
Tags serialization static class

Introduction to the Framework

MapReduce can only support writable do key,value? The answer is in the negative. In fact, all types are supported by just one small condition: each type is transmitted in binary streams. This Hadoop provides a serialization framework to support the types that writable can serve as mapreduce support in the Org.apache.hadoop.io.serializer package as well as the fact that there are not many classes, we start with several interfaces.

Serializer

Defines a set of interfaces, opens the stream, serializes, closes the stream

Public interface Serializer <T>  {  
    void open (Java.io.OutputStream outputstream) throws java.io.IOException;  
      
    void Serialize (T-T) throws java.io.IOException;  
      
    void Close () throws Java.io.IOException  
}

Deserializer

Defines a set of interfaces, opens the stream, deserializes it, and closes the stream

public interface 

Deserializer <T>  {  
    void open (Java.io.InputStream inputstream) throws java.io.IOException;  
      
    T Deserialize (T-T) throws java.io.IOException;  
      
    void Close () throws Java.io.IOException  
}

Serialization

Defines a set of interfaces that determine whether the input class is supported, and gives the serialization interface and the deserialization interface based on the input class

Public interface Serialization <T>  {  
    Boolean accept (java.lang.class<?> aclass);  
      
    Org.apache.hadoop.io.serializer.serializer<t> Getserializer (java.lang.class<t> 

tClass);  
      
    Org.apache.hadoop.io.serializer.deserializer<t> getdeserializer

(java.lang.class<t> tClass);  
}

Writableserialization

If you want to define a framework like writable yourself, then the first thing you need is to implement the three interfaces above, so let's take a look at how writable is implemented.

<pre style= "Word-wrap:break-word; White-space:pre-wrap;  
        
  ">public class Writableserialization extends configured implements serialization<writable> {  Static class Writabledeserializer extends configured implements deserializer<writable> {private  
    Class<?> Writableclass;  
          
    Private DataInputStream DataIn;  
      Public Writabledeserializer (Configuration conf, class<?> c) {setconf (conf);  
    This.writableclass = C; The public void open (InputStream in) {if (in instanceof datainputstream) {DataIn = (Dat  
      Ainputstream) in;  
      else {datain = new datainputstream (in);  
      } Public writable deserialize (writable W) throws IOException {writable writable;  
      if (w = = null) {writable = (writable) reflectionutils.newinstance (Writableclass, getconf ()); } else {Writable = W;  
      } writable.readfields (DataIn);  
    return writable;  
    public void Close () throws IOException {datain.close (); } Static class Writableserializer implements Serializer<writable> {Priva  
          
    Te DataOutputStream dataout;  public void Open (OutputStream out) {if (out instanceof dataoutputstream) {dataout = (DataOutputStream)  
      Out  
      else {dataout = new DataOutputStream (out);  
    } public void serialize (writable W) throws IOException {w.write (dataout);  
    public void Close () throws IOException {dataout.close ();  
  } public Boolean accept (class<?> c) {return Writable.class.isAssignableFrom (c); Deserializer<writable> Getdeserializer (class<writable> c) {return new Writabledeser Ializer (getconf(), c); Serializer<writable> Getserializer (class<writable> c) {return new writableserialize  
  R (); }}</pre>

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.