Hive支持多种数据存储的格式,序列化/反序列化只是其中的一种格式,其他类型可参考:https://www.cndba.cn/dave/article/3405
当进程在进行远程通信时,彼此可以发送各种类型的数据,无论是什么类型的数据都会以二进制序列的形式在网络上传送。发送方需要把对象转化为字节序列才可在网络上传输,称为对象序列化;接收方则需要把字节序列恢复为对象,称为对象的反序列化。序列化是对象转化为字节序列的过程;反序列化是字节码恢复为对象的过程;
序列化的作用主要有两个:
- 1、对象向的持久化;即把对象转换成字节码后保存文件;
- 2、对象数据的传输;
反序列化的主要作用:
- 对 反序列化成Hive table的每一列的值;Hive可以方便的将数据加载到表中而不需要对数据进行转换,这样在海量数据处理时,可以节省大量的时间。
在Hive的HQL语句中,select时将会用到反序列化操作, insert 时会用到序列化操作。
Hive创建表时,通过自定义的SerDe或使用Hive内置的SerDe类型指定数据的序列化和反序列化方式。
序列化和反序列化的目的
Serde是用于序列化和反序列化。
序列化的目的是Hive格式输出成为特定格式,包括:
分隔符(tab、逗号、CTRL-A)
Thrift 协议
反序列化的目的是HDFS格式读入Hive内存中,包括:
Java Integer/String/ArrayList/HashMap
Hadoop Writable 类
用户自定义类
序列化是Hive----->Hadoop, 反序列化是hadoop------>Hive 。本质的区别是数据存储的形式不同。
serialize() 接口:实现Object -> Writable的转换
deserialize()接口:实现Writable -> Object 的转换
/**
* Serialize an object by navigating inside the Object with the
* ObjectInspector. In most cases, the return value of this function will be
* constant since the function will reuse the Writable object. If the client
* wants to keep a copy of the Writable, the client needs to clone the
* returned value.
*/
//序列化操作,参数obj是hive中的对象,Writable 是hadoop中的数据格式描述抽象
public abstract Writable serialize(Object obj, ObjectInspector objInspector)
throws SerDeException;
。。。。。。。。。。略
/**
* Deserialize an object out of a Writable blob. In most cases, the return
* value of this function will be constant since the function will reuse the
* returned object. If the client wants to keep a copy of the object, the
* client needs to clone the returned value by calling
* ObjectInspectorUtils.getStandardObject().
*
* @param blob The Writable object containing a serialized object
* @return A Java object representing the contents in the blob.
*/
//反序列化 blob是hadoop中的数据抽象格式,返回的是Object是hive中的对象
public abstract Object deserialize(Writable blob) throws SerDeException;
..........略。。。。。。。
为什么要进行序列化和反序列化的操作呢?
对<key,value>反序列化成Hive table的每一列的值;Hive可以方便的将数据加载到表中而不需要对数据进行转换,这样在海量数据处理时,可以节省大量的时间。
在Hive的HQL语句中,select时将会用到序列化操作, insert 时会用到反序列化操作