- 浏览: 246000 次
-
文章分类
最新评论
引自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) ObjectWritable和GenericWritable
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.
发表评论
-
大数据方面的文章
2013-07-29 17:01 868http://bbs.e-works.net.cn/forum ... -
Apache Pig中文教程(进阶)
2013-05-13 17:18 1768引自http://www.codelast.com/?p=42 ... -
hadoop视频
2013-05-10 09:35 804http://pan.baidu.com/share/li ... -
Apache Pig的一些基础概念及用法总结(1
2013-05-08 16:01 1107引自http://www.codelast.com/?p=3 ... -
hadoop动态增加删除节点
2013-04-28 09:54 1191在master的conf/hdfs-site.xml中加入 ... -
hadoop 比较好的博客
2013-04-27 17:35 733http://dongxicheng.org 好的书 h ... -
Hadoop错误一的解决猜测
2013-04-26 10:29 843如果出现:java.lang.NullPointerExc ... -
Reduce作业运行时错误:Too many fetch-failures
2013-04-24 21:19 5794root@ubuntu:/usr/local/hadoop# ... -
MultipleOutputFormat和MultipleOutputs
2013-01-04 17:26 991引自http://www.cnblogs.com/liangz ... -
hadoop各种输入方法(InputFormat)汇总
2013-01-04 17:02 1424引自http://www.blogjava.net/shenh ... -
Hadoop运行报错: java.lang.ClassNotFoundException解决方法
2012-12-27 16:44 12812在创建自定义的Mapper时候,编译正确,但上传到集群执 ... -
hadoop-1.1.0 rpm + centos 6.3 64 + JDK7 搭建全分布式集群的方法
2012-12-22 20:45 1257引自 http://blog.csdn.net/ireland ... -
HADOOP中DATANODE无法启动
2012-12-22 20:43 963摘要:该文档解决了多次格式化文件系统后,datanode ... -
Hadoop HDFS 编程
2012-12-18 17:38 878引自http://blog.csdn.net/lmc ... -
HDFS之SequenceFile和MapFile
2012-12-17 11:37 956引自http://blog.csdn.net/javam ... -
hadoop问题Type mismatch in value from map解决方法
2012-12-13 10:49 874hadoop问题Type mismatch in ... -
hadoop hbase svn site
2012-12-13 10:49 1001hadoop hbase svn site ... -
hadoop项目svn地址
2012-12-11 18:11 1060http://svn.apache.org/repos/asf ... -
在Eclipse中导入hadoop
2012-12-11 18:03 12430. 准备 (1) 需要有gcc、autoconf、 ... -
Hadoop实例WordCount程序一步一步运行
2012-12-11 16:32 1010虽说现在用Eclipse下开发Hadoop程序很方便了,但是命 ...
相关推荐
4. **数据存储与序列化**:Hadoop使用`org.apache.hadoop.io`包处理数据的存储和序列化,包括各种基本类型(如IntWritable、Text等)和复杂的可序列化对象(如SequenceFile、Avro等)。 5. **配置管理**:`org....
但可以推测,这个扩展模块可能包含定制化的输入输出流、数据序列化/反序列化工具,或者是针对特定硬件或网络环境的优化。 标签 "开源项目" 表明了这些软件包遵循开源许可协议,允许用户自由地使用、修改和分发代码...
Hadoop,作为一个分布式计算框架,同样需要处理大量的数据,因此也引入了自己的序列化机制。Hadoop的序列化方式不同于Java的标准序列化,它使用了一种称为`Writable`的自定义格式。`Writable`接口设计得更加紧凑且...
5. **Avro API**:Hadoop 2.6还支持Avro数据序列化框架,允许跨语言的数据交换。`org.apache.hadoop.io.avro`包提供了Avro相关的类和接口。 6. **Guava库**:Hadoop依赖Google的Guava库,提供了一组实用工具类,如...
Java内置的序列化机制是通过实现`java.io.Serializable`接口来标记一个类可以被序列化。当对象实例需要序列化时,Java会调用`writeObject()`和`readObject()`方法来处理对象的状态。这个过程是自动的,开发者只需要...
深入浅析Java Object Serialization与 Hadoop 序列化 序列化是指将结构化对象转化为字节流以便在网络上传输或者写到磁盘永久存储的过程。Java 中的序列化是通过实现 Serializable 接口来实现的,而 Hadoop 序列化则...
这一机制主要体现在`org.apache.hadoop.io`包中的各类可序列化对象,它们实现了`Writable`接口。 - **示例代码**:以下是一个简单的`MyWritable`类实现`Writable`接口的例子,用于演示如何实现序列化与反序列化: `...
Hadoop使用Writables接口进行数据序列化,如`IntWritable`和`Text`等。然而,随着Java的序列化和protobuf等其他高效序列化框架的发展,现在更推荐使用这些现代序列化方式。开发者可能需要修改Hadoop的I/O层以支持新...
【Hadoop平台技术 序列化操作案例】 在Hadoop平台上进行大数据处理时,序列化是必不可少的一个环节,它允许我们将复杂的数据结构转化为字节流,以便在网络间传输或存储到磁盘。在这个案例中,我们将探讨如何在...
以上内容涵盖了Hadoop的基本概念、运行模式、生态系统组件、集群运行的关键进程以及数据处理中的序列化和切片机制,这些都是面试中常见的问题,理解和掌握这些知识点对于Hadoop开发者至关重要。
在Java中,如果一个类需要支持序列化,那么这个类就需要实现`java.io.Serializable`接口,尽管该接口没有任何方法需要实现,但它的存在就标志着这个类的对象可以被序列化。 首先,我们来看一下`SerializableDemo`这...
4. **ProtocolBuffer**:Google的一种高效的数据交换格式,Hadoop需要它来支持数据序列化等功能。 5. **CMake**:一个跨平台的自动化构建系统,通常用于编译C/C++项目。 6. **ZLib**:一个提供数据压缩功能的库。 7....
Lib包是Hadoop的核心依赖库,包含了各种JAR文件,它们是Hadoop运行的基石,涵盖了网络通信、序列化、I/O操作、分布式存储和计算等关键功能。 lib包中包含了以下几类关键库文件: 1. **Hadoop Core Libraries**:...
Hadoop并没有使用Java的序列化机制,而是自己定义了大量的可序列化对象,这些对象都实现了Writable接口。实现Writable接口的类可以将对象数据编码成适合网络传输的格式,并能够从这种格式中解码。 Hadoop的...
- io.file.buffer.size:指的是序列化文件的缓冲大小,它应设置为硬件页面大小的倍数,例如在x86架构上通常是4096字节。 - file.blocksize:定义了HDFS的默认数据块大小,128MB(***字节)是默认值。 hdfs-site.xml...
在深入分析Hadoop源代码的过程中,我们关注到其在数据通信和序列化方面的重要机制。Hadoop并没有简单地采用Java自带的序列化方法,而是设计了一套自有的系统,这主要是为了提高性能、可扩展性和跨平台兼容性。在...
最后,由于使用同步阻塞IO,其性能相比现代序列化协议较低。 2. **XML**:XML是一种常见的序列化协议,具有良好的可读性和自定义元素特性。然而,XML序列化数据仅包含数据本身,不包括类型信息,且需要有默认构造...
这种机制主要集中在`org.apache.hadoop.io`包中,定义了一系列可序列化的对象,并要求这些对象实现`Writable`接口。例如: ```java public class MyWritable implements Writable { private int counter; private...