hive表的源文件存储格式:
1、textfile
默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理。源文件可以直接通过hadoop fs -cat 查看
2、sequencefile
一种hadoop api提供的二进制文件,使用方便、可分割、可压缩等特点。 sequencefile将数据以<key,value>的形式序列化到文件中。序列化和反序列化使用hadoop 的标准的writable 接口实现。key为空,用value 存放实际的值, 这样可以避免map 阶段的排序过程。
三种压缩选择:none, record, block。 record压缩率低,一般建议使用block压缩。使用时设置参数,
- set hive.exec.compress.output=true;
- set io.seqfile.compression.type=block; -- none/record/block
- create table test2(str string) stored as sequencefile;
3、rcfile
一种行列存储相结合的存储方式。首先,其将数据按行分块,保证同一个record在一个块上,避免读一个记录需要读取多个block。其次,块数据列式存储,有利于数据压缩和快速的列存取。理论上具有高查询效率(但hive官方说效果不明显,只有存储上能省10%的空间,所以不好用,可以不用)。 rcfile结合行存储查询的快速和列存储节省空间的特点
1)同一行的数据位于同一节点,因此元组重构的开销很低;
2) 块内列存储,可以进行列维度的数据压缩,跳过不必要的列读取。
查询过程中,在io上跳过不关心的列。实际过程是,在map阶段从远端拷贝仍然拷贝整个数据块到本地目录,也并不是真正直接跳过列,而是通过扫描每一个row group的头部定义来实现的。
但是在整个hdfs block 级别的头部并没有定义每个列从哪个row group起始到哪个row group结束。所以在读取所有列的情况下,rcfile的性能反而没有sequencefile高。
4、orc
hive给出的新格式,属于rcfile的升级版。
5、自定义格式
用户的数据文件格式不能被当前 hive 所识别的,时通过实现inputformat和outputformat来自定义输入输出格式
注意:
只有textfile表能直接加载数据,必须,本地load数据,和external外部表直接加载运路径数据,都只能用textfile表。 更深一步,hive默认支持的压缩文件(hadoop默认支持的压缩格式),也只能用textfile表直接读取。其他格式不行。可以通过textfile表加载后insert到其他表中。
换句话说,sequencefile、rcfile表不能直接加载数据,数据要先导入到textfile表,再从textfile表通过insert select from 导入到sequencefile,rcfile表。 sequencefile、rcfile表的源文件不能直接查看,在hive中用select看。
rcfile源文件可以用 hive --service rcfilecat /xxxxxxxxxxxxxxxxxxxxxxxxxxx/000000_0查看,但是格式不同,很乱。
orc格式
orc是rcfile的升级版,性能有大幅度提升, 而且数据可以压缩存储,压缩比和lzo压缩差不多,比text文件压缩比可以达到70%的空间。而且读性能非常高,可以实现高效查询。 具体介绍https://cwiki.apache.org/confluence/display/hive/languagemanual+orc
建表语句如下: 同时,将orc的表中的null取值,由默认的\n改为'',
方式一:
create table if not exists test_orc(
advertiser_id string,
ad_plan_id string,
cnt bigint
) partitioned by (day string, type tinyint comment '0 as bid, 1 as win, 2 as ck', hour tinyint)
stored as orc;
alter table test_orc set serdeproperties('serialization.null.format' = '');查看结果
hive> show create table test_orc; create table `test_orc`( `advertiser_id` string, `ad_plan_id` string, `cnt` bigint) partitioned by ( `day` string, `type` tinyint comment '0 as bid, 1 as win, 2 as ck', `hour` tinyint) row format delimited null defined as '' stored as inputformat 'org.apache.hadoop.hive.ql.io.orc.orcinputformat' outputformat 'org.apache.hadoop.hive.ql.io.orc.orcoutputformat' location 'hdfs://namenode/hivedata/warehouse/pmp.db/test_orc' tblproperties ( 'last_modified_by'='pmp_bi', 'last_modified_time'='1465992624', 'transient_lastddltime'='1465992624')
方式二:
drop table test_orc;
create table if not exists test_orc(
advertiser_id string,
ad_plan_id string,
cnt bigint
) partitioned by (day string, type tinyint comment '0 as bid, 1 as win, 2 as ck', hour tinyint)
row format serde
'org.apache.hadoop.hive.ql.io.orc.orcserde'
with serdeproperties('serialization.null.format' = '')
stored as orc;查看结果
hive> show create table test_orc; create table `test_orc`( `advertiser_id` string, `ad_plan_id` string, `cnt` bigint) partitioned by ( `day` string, `type` tinyint comment '0 as bid, 1 as win, 2 as ck', `hour` tinyint) row format delimited null defined as '' stored as inputformat 'org.apache.hadoop.hive.ql.io.orc.orcinputformat' outputformat 'org.apache.hadoop.hive.ql.io.orc.orcoutputformat' location 'hdfs://namenode/hivedata/warehouse/pmp.db/test_orc' tblproperties ( 'transient_lastddltime'='1465992726')
方式三:
drop table test_orc; create table if not exists test_orc( advertiser_id string, ad_plan_id string, cnt bigint ) partitioned by (day string, type tinyint comment '0 as bid, 1 as win, 2 as ck', hour tinyint) row format delimited null defined as '' stored as orc;
查看结果
hive> show create table test_orc; create table `test_orc`( `advertiser_id` string, `ad_plan_id` string, `cnt` bigint) partitioned by ( `day` string, `type` tinyint comment '0 as bid, 1 as win, 2 as ck', `hour` tinyint) row format delimited null defined as '' stored as inputformat 'org.apache.hadoop.hive.ql.io.orc.orcinputformat' outputformat 'org.apache.hadoop.hive.ql.io.orc.orcoutputformat' location 'hdfs://namenode/hivedata/warehouse/pmp.db/test_orc' tblproperties ( 'transient_lastddltime'='1465992916')
到此这篇关于关于hive表的存储格式orc格式的使用详解的文章就介绍到这了,更多相关hive表的orc格式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论