`

Hadoop -【IO专题-序列化机制】

 
阅读更多

 

引自http://blog.sina.com.cn/s/blog_48a45b950100sz4x.html

 

1. 基本概念 

       序列化可被定义为将对象的状态存储到存储媒介中的过程。在此过程中,对象的公共字段和私有字段以及类的名称(包括包含该类的程序集)都被转换为字节流,然后写入数据流。在以后反序列化该对象时,创建原始对象的精确复本。

       当在面向对象的环境中实现序列化机制时,您需要在简化使用和保持灵活性之间进行许多权衡。只要您对该过程具有充分的控制,就可以在很大程度上自动化该过程。例如,在简单二进制序列化不充分时可能导致一些情况发生,或者可能有特定原因确定在类中哪些字段需要进行序列化。

       Serialization is the process of turning structured objects into a byte stream for trans-mission over a network or for writing to persistent storage. Deserialization is the process of turning a byte stream back into a series of structured objects.

       Serialization appears in two quite distinct areas of distributed data processing: forinterprocess communication and for persistent storage.

       In Hadoop, interprocess communication between nodes in the system is implemented usingremote procedure calls (RPCs). The RPC protocol uses serialization to render the message into a binary stream to be sent to the remote node, which then deserializes the binary stream into the original message. In general, it is desirable that an RPC serialization format is:

       ·  数据设计紧凑,充分利用网络带宽

       ·  系列化和反序列化的工程能够迅速完成

       ·  协议是高可扩展的

       ·  支持互操作

       Hadoop的系列化是通过Writable接口来实现的,只满足了前两条设计,在org.apache.hadoop.io包下包含了大量的可序列化的组件,它们都实现了Writable接口,Writable接口提供了两个方法,write和readFields,分别用来序列化和反序列化,实现该接口的典型例子如下:

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;

    }

}

 

2. 比较器 

在Map/Reduce中有多处要进行排序,所以元素的相互比较是比较重要的,Hadoop中通过如下几个接口和类来支持比较操作:

1)  WritableComparable

public interface WritableComparable<T> extends Writable, Comparable<T> {

}

2)  RawComparator

public interface RawComparator<T> extends Comparator<T> {

    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2);

}

This interface permits implementors to compare records read from a stream without

deserializing them into objects, thereby avoiding any overhead of object creation.

3)  WritableComparator

WritableComparator is a general-purpose implementation of  RawComparator for WritableComparable classes. It provides two main functions 

First, it provides a default implementation of the raw compare() method that deserializes the objects to be compared from the stream and invokes the object compare() method.

Second, it acts as a factory for RawComparator instances (that Writable implementations haveregistered,通过一个HashMap<Class, WritableComparator> comparators来保持所有注册的comparator).For example, to obtain a comparator for IntWritable, we just use:

    RawComparator<IntWritable> comparator = WritableComparator.get(IntWritable.class);

    获得这个comparator之后就可以来比较两个IntWritable的对象:

    IntWritable w1 = new IntWritable(100);

    IntWritable w2 = new IntWritable(101);

    assertThat(comparator.compare(w1,w2), greatherThan(0));

    或者对序列化的对象进行比较:

    byte[] b1 = serialize(w1);

    byte[] b2 = serialize(w2);

    assertThat(comparator.compare(b1,0,b1.length, b2, 0, b2.length), greatherThan(0));

 

3. Writable Classes

Hadoop提供的可序列化的类型主要分成如下几种:

1)  Java基本数据类型 

There are  Writable wrappers  for all  the  Java primitive  types  except short and char (both of which can be stored in an IntWritable). All have a get() and a set() method for retrieving and storing the wrapped value.

Java 基本数据类型

Writable实现

Serialized size(bytes)

boolean

BooleanWritable

1

byte

ByteWritable

1

int

IntWritable

4


VIntWritable

1-5

float

FloatWritable

4

long

LongWritable

8


VLongWritable

1-9

double

DoubleWritable

8

2)  Text

Text is a Writable for UTF-8 sequences, the max length is 2GB, 这个类的系列化和反序列化都是比较显而易见的,实现上大部分代码在做 UTF-8编解码和String(java中的String用的是unicode)的转换等。

3)  BytesWritable

BytesWritable is a wrapper for an array of binary data. Its serialized format is an integerfield (4 bytes) that specifies the number of bytes to follow, followed by the bytes them-selves. For example, the byte array of length two with values 3 and 5 is serialized as a 4-byte integer (00000002) followed by the two bytes from the array (03 and 05):

    BytesWritable b = new BytesWritable(new byte[] { 3, 5 });

    byte[] bytes = serialize(b);

    assertThat(StringUtils.byteToHexString(bytes), is("000000020305"));

    BinaryComparable是针对array of binary data而设计的比较器!

4)  NullWritable

NullWritable is a special type of Writable, as it has a zero-length serialization. No bytes are written to, or read  from, the stream. It  is used as a placeholder; for example, in MapReduce, a key or a value can be declared as a NullWritable when you don’t need to use that position—it effectively stores a constant empty value.

5)  ObjectWritableGenericWritable

ObjectWritable is a general-purpose wrapper for the following: Java primitives, String, enum, Writable, null, or arrays of any of these types. It is used in Hadoop RPC to marshal(包装) and unmarshal method arguments and return types. 其实主要的通途就是对多于1个的域组成对象进行序列化!在对端进行反序列化的时候用到了WritableFactory和 WritableFactories(用来根据类名来生成对象)

6)  Writable Collections

There are four Writable collection types in the org.apache.hadoop.io package:

    ArrayWritable, TwoDArrayWritable, MapWritable, and SortedMapWritable.

ArrayWritable and  TwoDArrayWritable  are  Writable  implementations  for  arrays  and two-dimensional arrays (array of arrays) of Writable instances. All the elements of an ArrayWritable or a TwoDArrayWritable must be  instances of  the same class, which  is specified at construction, as follows:

    ArrayWritable writable = new ArrayWritable(Text.class);

MapWritable and SortedMapWritable are implementations of java.util.Map<Writable,Writable> and java.util.SortedMap<WritableComparable, Writable>, respectively. The type of each key and value field is a part of the serialization format for that field.

Hadoop <wbr>-【IO专题-序列化机制】

 

  • 大小: 57.7 KB
分享到:
评论

相关推荐

    hadoop-core-0.20.2 源码 hadoop-2.5.1-src.tar.gz 源码 hadoop 源码

    4. **数据存储与序列化**:Hadoop使用`org.apache.hadoop.io`包处理数据的存储和序列化,包括各种基本类型(如IntWritable、Text等)和复杂的可序列化对象(如SequenceFile、Avro等)。 5. **配置管理**:`org....

    hadoop-yarn-common-0.23.9.zip

    但可以推测,这个扩展模块可能包含定制化的输入输出流、数据序列化/反序列化工具,或者是针对特定硬件或网络环境的优化。 标签 "开源项目" 表明了这些软件包遵循开源许可协议,允许用户自由地使用、修改和分发代码...

    java-Hadoop序列化

    Hadoop,作为一个分布式计算框架,同样需要处理大量的数据,因此也引入了自己的序列化机制。Hadoop的序列化方式不同于Java的标准序列化,它使用了一种称为`Writable`的自定义格式。`Writable`接口设计得更加紧凑且...

    hadoop2.6-api.zip

    5. **Avro API**:Hadoop 2.6还支持Avro数据序列化框架,允许跨语言的数据交换。`org.apache.hadoop.io.avro`包提供了Avro相关的类和接口。 6. **Guava库**:Hadoop依赖Google的Guava库,提供了一组实用工具类,如...

    几种序列化的实现方法 java自带, Writable, Avro

    Java内置的序列化机制是通过实现`java.io.Serializable`接口来标记一个类可以被序列化。当对象实例需要序列化时,Java会调用`writeObject()`和`readObject()`方法来处理对象的状态。这个过程是自动的,开发者只需要...

    深入浅析Java Object Serialization与 Hadoop 序列化

    深入浅析Java Object Serialization与 Hadoop 序列化 序列化是指将结构化对象转化为字节流以便在网络上传输或者写到磁盘永久存储的过程。Java 中的序列化是通过实现 Serializable 接口来实现的,而 Hadoop 序列化则...

    Hadoop源码分析 完整版 共55章

    这一机制主要体现在`org.apache.hadoop.io`包中的各类可序列化对象,它们实现了`Writable`接口。 - **示例代码**:以下是一个简单的`MyWritable`类实现`Writable`接口的例子,用于演示如何实现序列化与反序列化: `...

    修改hadoop中的io写的,远程调用对象的东西。

    Hadoop使用Writables接口进行数据序列化,如`IntWritable`和`Text`等。然而,随着Java的序列化和protobuf等其他高效序列化框架的发展,现在更推荐使用这些现代序列化方式。开发者可能需要修改Hadoop的I/O层以支持新...

    Hadoop平台技术 序列化操作案例.docx

    【Hadoop平台技术 序列化操作案例】 在Hadoop平台上进行大数据处理时,序列化是必不可少的一个环节,它允许我们将复杂的数据结构转化为字节流,以便在网络间传输或存储到磁盘。在这个案例中,我们将探讨如何在...

    最新Hadoop的面试题总结

    以上内容涵盖了Hadoop的基本概念、运行模式、生态系统组件、集群运行的关键进程以及数据处理中的序列化和切片机制,这些都是面试中常见的问题,理解和掌握这些知识点对于Hadoop开发者至关重要。

    Java实现序列化例子

    在Java中,如果一个类需要支持序列化,那么这个类就需要实现`java.io.Serializable`接口,尽管该接口没有任何方法需要实现,但它的存在就标志着这个类的对象可以被序列化。 首先,我们来看一下`SerializableDemo`这...

    hadoop源码编译

    4. **ProtocolBuffer**:Google的一种高效的数据交换格式,Hadoop需要它来支持数据序列化等功能。 5. **CMake**:一个跨平台的自动化构建系统,通常用于编译C/C++项目。 6. **ZLib**:一个提供数据压缩功能的库。 7....

    hadoop lib包

    Lib包是Hadoop的核心依赖库,包含了各种JAR文件,它们是Hadoop运行的基石,涵盖了网络通信、序列化、I/O操作、分布式存储和计算等关键功能。 lib包中包含了以下几类关键库文件: 1. **Hadoop Core Libraries**:...

    Hadoop源码分析(完整版)

    Hadoop并没有使用Java的序列化机制,而是自己定义了大量的可序列化对象,这些对象都实现了Writable接口。实现Writable接口的类可以将对象数据编码成适合网络传输的格式,并能够从这种格式中解码。 Hadoop的...

    hadoop配置文件详解

    - io.file.buffer.size:指的是序列化文件的缓冲大小,它应设置为硬件页面大小的倍数,例如在x86架构上通常是4096字节。 - file.blocksize:定义了HDFS的默认数据块大小,128MB(***字节)是默认值。 hdfs-site.xml...

    Hadoop源代码分析(三)

    在深入分析Hadoop源代码的过程中,我们关注到其在数据通信和序列化方面的重要机制。Hadoop并没有简单地采用Java自带的序列化方法,而是设计了一套自有的系统,这主要是为了提高性能、可扩展性和跨平台兼容性。在...

    js序列化架构师必懂的——七种序列化机制及技术选型.docx

    最后,由于使用同步阻塞IO,其性能相比现代序列化协议较低。 2. **XML**:XML是一种常见的序列化协议,具有良好的可读性和自定义元素特性。然而,XML序列化数据仅包含数据本身,不包括类型信息,且需要有默认构造...

    Hadoop源代码分析(完整版)

    这种机制主要集中在`org.apache.hadoop.io`包中,定义了一系列可序列化的对象,并要求这些对象实现`Writable`接口。例如: ```java public class MyWritable implements Writable { private int counter; private...

Global site tag (gtag.js) - Google Analytics