Hive系列之SerDe

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hive系列之SerDe相关的知识,希望对你有一定的参考价值。

参考技术A SerDe是Serialize/Deserilize的简称,目的是用于序列化和反序列化。

序列化是对象转换为字节序列的过程。
序列化是字节序列恢复为对象的过程。
对象的序列化主要有两种用途:对象的持久化,即把对象转换成字节序列后保存到文件中;对象数据的网络传送。
除了上面两点, hive的序列化的作用还包括:Hive的反序列化是对key/value反序列化成hive table的每个列的值。Hive可以方便的将数据加载到表中而不需要对数据进行转换,这样在处理海量数据时可以节省大量的时间。

SerDe说明hive如何去处理一条记录,包括Serialize/Deserilize两个功能, Serialize把hive使用的java object转换成能写入hdfs的字节序列,或者其他系统能识别的流文件。Deserilize把字符串或者二进制流转换成hive能识别的java object对象。比如:select语句会用到Serialize对象, 把hdfs数据解析出来;insert语句会使用Deserilize,数据写入hdfs系统,需要把数据序列化。

hive创建表时, 通过自定义的SerDe或使用Hive内置的SerDe类型指定数据的序列化和反序列化方式。

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)]
INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]

如上创建表语句, 使用row format 参数说明SerDe的类型。

Avro
ORC
RegEx
Thrift
Parquet
CSV
JsonSerDe

自定义类型使用的步骤:

Hive之SerDe&Beeline

一.SerDe 

  SerDe:Serializer and Deserializer 序列化及反序列化,构建在数据存储和执行引擎之间,对两者实现解耦。

  Hive通过ROW FORMAT DELIMITED 以及SERDE进行内容的读写。

  样板:

  row format

  :DELIMITED 

    [FIELDS TERMIINATED BY char [ESCAPED BY char]

    [COLLECTION ITEMS TERMINATED BY char]

    [MAP KEYS TERMINATED BY char]

    [LINES TERMINATED BY char]

  :SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value,property_name=property_value...)]

  案例1:

    create table zhen(id int, name string,age int)

    row format delimited fields terminated by ‘,‘  collection items terninated by ‘,‘ map keys terminated by ‘:‘ lines terminated by ‘\n‘ ;

  案例2:

    create table zhen(id int, name string,age int)

    row format serde ‘org.apache.hadoop.hive.serde2.RegexSerDe‘ with serdeproperties("input.regex" = "([^ ])([^ ])([^ ])") // 正则匹配

    stored as textfile;

  备注:lines 默认按照换行拆分!

二.Beeline

  格式化数据表数据,Beeline要和HiveServer2配合使用。服务端启动hiveserver2,客户端通过beeline两种方式连接hive

  1.beeline -u jdbc:hive2://localhost:10000/default -n root

  2.1.beeline

  2.2 !connect jebc:hive2:/localhost:10000/defalut;auth-noSasl root 123

 

  

以上是关于Hive系列之SerDe的主要内容,如果未能解决你的问题,请参考以下文章

Hive之序列化与反序列化(SerDe)

大数据系列之数据仓库Hive原理

大数据学习系列之五 ----- Hive整合HBase图文详解

Hive 如何存储数据,啥是 SerDe?

Hive源码系列编译模块之整体介绍

无法验证 serde:org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe