您的位置:首页 > 其它

hive表存储格式及压缩

2016-10-13 00:00 183 查看

1.textfile

Hive数据表的默认格式,磁盘开销大,数据解析开销大

存储方式:行存储

压缩方式:使用Gzip,Bzip2等压缩算法压缩,压缩后的文件不支持split

但在反序列化过程中,必须逐个字符判断是不是分隔符和行结束符,因此反序列化开销会比SequenceFile高几十倍。

--创建数据表:
create table if not exists textfile_table(
site string,
url  string,
pv   bigint,
label string)
row format delimited fields terminated by '\t'
stored as textfile;
--插入数据:
set hive.exec.compress.output=true; --启用压缩格式
set mapred.output.compress=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  --指定输出的压缩格式为Gzip
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
insert overwrite table textfile_table select * from T_Name;


2.sequencefile

Hadoop API提供的一种二进制文件,以<key,value>的形式序列化到文件中

存储方式:行存储

压缩方式:NONE,RECORD,BLOCK。Record压缩率低,一般建议使用BLOCK压缩。

优势是文件和hadoop api中的MapFile是相互兼容的

create table if not exists seqfile_table(
site string,
url  string,
pv   bigint,
label string)
row format delimited fields terminated by '\t'
stored as sequencefile;
--插入数据操作:
set hive.exec.compress.output=true;  --启用输出压缩格式
set mapred.output.compress=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  --指定输出压缩格式为Gzip
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
SET mapred.output.compression.type=BLOCK; --指定为Block
insert overwrite table seqfile_table select * from T_Name;



3.rcfile

存储方式:数据按行分组,每组内按列存储,默认行组大小是4MB

行列混合存储的优点:

同一行的数据位于同一节点,因此元组重构的开销很低

与列存一样,RCFile 能够利用列维度的数据压缩,并跳过不必要的列读取

RCFile的一个行组包括三个部分:

第一部分是行组头部的【同步标识】,主要用于分隔 hdfs 块中的两个连续行组

第二部分是行组的【元数据头部】,用于存储行组单元的信息,包括行组中的记录数、每个列的字节数、列中每个域的字节数

第三部分是【表格数据段】,即实际的列存储数据。在该部分中,同一列的所有域顺序存储。
从图可以看出,首先存储了列 A 的所有域,然后存储列 B 的所有域等



create table if not exists rcfile_table(
site string,
url  string,
pv   bigint,
label string)
row format delimited fields terminated by '\t'
stored as rcfile;
--插入数据操作:
set hive.exec.compress.output=true;
set mapred.output.compress=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
insert overwrite table rcfile_table select * from T_Name;


4.orcfile

存储方式:数据按行分组,每块按照列存储,默认行组大小为250MB

压缩方式:ZLIB和SNAPPY,默认ZLIB。压缩速度快 快速列存取

效率比rcfile高,是rcfile的改良版本

运用ORC File可以提高Hive的读、写以及处理数据的性能。
与RCFile格式相比,ORCFile有以下优点:
  (1)、每个task只输出单个文件,这样可以减少NameNode的负载
  (2)、支持复杂的数据类型,如: datetime, decimal, 以及一些复杂类型(struct, list, map, and union)
  (3)、在文件中存储了一些轻量级的索引数据
  (4)、基于数据类型的块模式压缩:integer类型列用行程长度编码;String类型列用字典编码;
  (5)、用多个互相独立的RecordReaders并行读相同的文件
  (6)、无需扫描markers就可以分割文件
  (7)、绑定读写所需要的内存
  (8)、metadata的存储是用 Protocol Buffers的,所以它支持添加和删除一些列

create table Addresses (
name string,
street string,
city string
)
row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="NONE");


5.自定义格式

用户可以通过实现inputformat和 outputformat来自定义输入输出格式。

create table myfile_table(str STRING)
stored as
inputformat 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextInputFormat'
outputformat 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextOutputFormat';


查询测试

实验所用表为15列,共约300多条非重复记录,总记录数20123648

格式大小可分割*col5count(*)max(c1)max(c1) where c5='xxx'
textfile1.3GB31s21s23s25s26s
rc-none1.0GB31s20s22s25s23s
rc-gzip1.6MB27s1s1s31s28s
orc-none53.3MB20s1s1s21s22s
orc-zlib645.2KB20s1s1s21s21s
实验所用表为72列,皆为非重复记录,总记录数为7644851

格式大小可分割* c5 count(*)max(c5)max(c5) where c9='T'
c2,count(*) group by c2
textfile3.3G27101212728
seq-none3.46G2571212168
seq-block0.91G471610101010
orc-none1.7G3755558
orc-zlib0.66G3768558
数据仓库的特点:一次写入、多次读取、并行执行,因此,整体来看,ORCFile相比其他格式具有较明显的优势。

TextFile 默认格式加载速度最快,可采用Gzip、bzip2等压缩,压缩后的无法split,无法并行处理

SequenceFile 压缩率最低,查询速度一般,三种压缩格式NONE,RECORD,BLOCK

RCfile 压缩率高,查询速度最快,数据加载最慢

ORCfile 压缩率高,查询速度快,数据加载快
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐