Serde是 Serializer/Deserializer
的简写。hive使用Serde进行行对象的序列与反序列化。最后实现把文件内容映射到 hive 表中的字段数据类型。
为了更好的阐述使用 SerDe 的场景,我们需要了解一下 Hive 是如何读数据的(类似于 HDFS 中数据的读写操作):
HDFS files –> InputFileFormat –> –> Deserializer –> Row object
Row object –> Serializer –> –> OutputFileFormat –> HDFS files
org.apache.hadoop.hive.serde2
库,内部封装了很多不同的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的类型
.https://github.com/Raray-chuan/xichuan_blog_pic/blob/main/file/hive/hive-log.txt
log.txt 大小为18.1 M
create table log_text (
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as textfile ;
load data local inpath '/home/hadoop/log.txt' into table log_text ;
dfs -du -h /user/hive/warehouse/log_text;
+------------------------------------------------+--+
| DFS Output |
+------------------------------------------------+--+
| 18.1 M /user/hive/warehouse/log_text/log.txt |
+------------------------------------------------+--+
create table log_parquet (
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as parquet;
insert into table log_parquet select * from log_text;
dfs -du -h /user/hive/warehouse/log_parquet;
+----------------------------------------------------+--+
| DFS Output |
+----------------------------------------------------+--+
| 13.1 M /user/hive/warehouse/log_parquet/000000_0 |
+----------------------------------------------------+--+
create table log_orc (
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as orc ;
insert into table log_orc select * from log_text ;
dfs -du -h /user/hive/warehouse/log_orc;
+-----------------------------------------------+--+
| DFS Output |
+-----------------------------------------------+--+
| 2.8 M /user/hive/warehouse/log_orc/000000_0 |
+-----------------------------------------------+--+
ORC > Parquet > textFile
select count(*) from log_text;
+---------+--+
| _c0 |
+---------+--+
| 100000 |
+---------+--+
1 row selected (16.99 seconds)
select count(*) from log_parquet;
+---------+--+
| _c0 |
+---------+--+
| 100000 |
+---------+--+
1 row selected (17.994 seconds)
select count(*) from log_orc;
+---------+--+
| _c0 |
+---------+--+
| 100000 |
+---------+--+
1 row selected (15.943 seconds)
ORC > TextFile > Parquet
使用压缩的优势是可以最小化所需要的磁盘存储空间,以及减少磁盘和网络io操作
官网地址
ORC支持三种压缩:ZLIB,SNAPPY,NONE。最后一种就是不压缩,orc默认采用的是ZLIB压缩
。
create table log_orc_none (
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as orc tblproperties("orc.compress"="NONE") ;
insert into table log_orc_none select * from log_text ;
dfs -du -h /user/hive/warehouse/log_orc_none;
+----------------------------------------------------+--+
| DFS Output |
+----------------------------------------------------+--+
| 7.7 M /user/hive/warehouse/log_orc_none/000000_0 |
+----------------------------------------------------+--+
create table log_orc_snappy (
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as orc tblproperties("orc.compress"="SNAPPY") ;
insert into table log_orc_snappy select * from log_text ;
dfs -du -h /user/hive/warehouse/log_orc_snappy;
+------------------------------------------------------+--+
| DFS Output |
+------------------------------------------------------+--+
| 3.8 M /user/hive/warehouse/log_orc_snappy/000000_0 |
+------------------------------------------------------+--+
dfs -du -h /user/hive/warehouse/log_orc;
+-----------------------------------------------+--+
| DFS Output |
+-----------------------------------------------+--+
| 2.8 M /user/hive/warehouse/log_orc/000000_0 |
+-----------------------------------------------+--+
orc 默认的压缩方式ZLIB比Snappy压缩的还小。
在实际的项目开发当中,hive表的数据存储格式一般选择:orc或parquet。
由于snappy的压缩和解压缩 效率都比较高,压缩方式一般选择snappy