Hadoop Source Code Analysis (iii) serialization of objects

Source: Internet
Author: User

Because of the need for communication between the MapReduce and HDFs of Hadoop, the communication object needs to be serialized. Hadoop does not use Java serialization, but rather
introduced its own system.
A large number of serializable objects are defined in Org.apache.hadoop.io, and they all implement the writable interface. A typical example of a writable interface is implemented, such as
Under
Java Code
1. Public class Mywritable implements writable {
2.//Some Data
3. private int counter;
4. Private long timestamp;
5.
6. public void Write (DataOutput out) throws IOException {
7. Out.writeint (counter);
8. Out.writelong (timestamp);
9.}
10.
One. public void ReadFields (Datainput in) throws IOException {
Counter = In.readint ();
timestamp = In.readlong ();
14.}
15.
Mywritable public static-read (Datainput in) throws IOException {
mywritable w = new mywritable ();
W.readfields (in);
. Return W;
20.}
21.}
The Write and readfields respectively implement the functions of serializing and deserializing objects, and are the two methods of writable interface definition. Gives a huge
The relationship of the objects in the Org.apache.hadoop.io.


Here, I marked the objectwritable as red because it has a different status than other objects. When we discuss the RPC of Hadoop, we refer to the information exchanged on RPC, which must be the basic type of Java, the implementation class for the String and writable interfaces, and an array of the above types for the elements. The Objectwritable object holds the type information for objects and objects that can be transferred on the RPC. In this way, we have a versatile writable object that can be used for client/server transfer. For example, we want to use the object in the above example as an RPC request, we need to create a objectwritable,objectwritable based on Mywritable to write the following information in the stream
Object class name length, object class name, object's own serialization result
So, to the opposite end, objectwritable can create the corresponding object according to the object class name and solve the serial. It should be noted that objectwritable relies on writablefactories, which

The plant that corresponds to the writable sub-class is stored. We need to keep the Mywritable factory in Writablefactories (via Writablefactories.setfactory).

For more information, please pay attention to: http://bbs.superwu.cn attention to Superman Academy: Bj-crxy

Hadoop Source Code Analysis (iii) serialization of objects

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.