hive的存储格式

1) 四种存储格式

hive的存储格式分为两大类:一类纯文本文件,一类是二进制文件存储。

Hive支持的存储数据的格式主要有:TEXTFILE、SEQUENCEFILE、ORC、PARQUET

第一类:纯文本文件存储

textfile: 纯文本文件存储格式,不压缩,也是hive的默认存储格式,磁盘开销大,数据解析开销大

第二类:二进制文件存储

  • sequencefile:

会压缩,不能使用load方式加载数据

  • parquet:

会压缩,不能使用load方式加载数据

  • rcfile:

会压缩,不能load。查询性能高,写操作慢,所需内存大,计算量大。此格式为行列混合存储,hive在该格式下,会尽量将附近的行和列的块存储到一起。

  • orcfile:rcfile的升级版。

2)列式存储和行式存储

TEXTFILE和SEQUENCEFILE的存储格式都是基于行存储的;

ORC和PARQUET是基于列式存储的。

行式存储:查找某一条整行数据比较快

列式存储:查找某个字段比较快 select name from user;

修改hive的默认存储格式:

复制代码
<property>
  <name>hive.default.fileformat</name>
  <value>TextFile</value>
  <description>
    Expects one of [textfile, sequencefile, rcfile, orc].
    Default file format for CREATE TABLE statement. Users can explicitly override it by CREATE TABLE ... STORED AS [FORMAT]
  </description>
</property>

也可以使用set方式修改:
set hive.default.fileformat=TextFile

textfile类型演示:

复制代码
create table stocks_1 (
  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/hivedata/stocks.log' into table stocks_1;
在linux的命令行上使用hdfs dfs -put方法去上传到指定目录下。

可以查看到数据,说明是文本类型的。

sequencefile 的使用

复制代码
create external table if not exists stocks_seq_1 (
  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 sequencefile;

由于不能load数据,从普通表中查询出来插入进入。
使用insert into的方式加载数据
insert into stocks_seq_1 select * from stocks_1 ;
或者使用克隆的方式:
create table stocks_seq_2 stored as sequencefile as select * from stocks_1;

查看数据,是乱码,说明是二进制文件

parquetfile 类型

复制代码
create external table if not exists stocks_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的方式加载数据
insert into stocks_parquet select * from stocks_1 ;
或者使用克隆的方式:
create table stocks_parquet_1 stored as parquet as select * from stocks_1;

rcfile类型:

复制代码
create external table if not exists stocks_rcfile (
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 rcfile;

使用insert into的方式加载数据
insert into stocks_rcfile select * from stocks_1;
或者使用克隆的方式:
create table stocks_rcfile_2 stored as rcfile as select * from stocks_1;

orcfile类型:rcfile的升级版

复制代码
create external table if not exists stocks_orcfile (
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 ','
stored as orcfile;

使用insert into的方式加载数据
insert into stocks_orcfile select * from stocks_1;
或者使用克隆的方式:
create table stocks_orcfile_2 stored as orcfile as select * from stocks_1;

查询速度和压缩比例对比:

复制代码
select count(*) from stocks_1;
select count(*) from stocks_seq_1;
select count(*) from stocks_parquet;       
select count(*) from stocks_rcfile;
select count(*) from stocks_orcfile;
比较一下上述五个查询所需要的时间

文件存储格式是真正的压缩吗? 每一种文件文件格式有一定的压缩比例,但是不是真正的压缩,而是文件格式带来的。

相关推荐
打工的小王20 分钟前
redis(四)搭建哨兵模式:一主二从三哨兵
数据库·redis·缓存
Anarkh_Lee41 分钟前
【小白也能实现智能问数智能体】使用开源的universal-db-mcp在coze中实现问数 AskDB智能体
数据库·人工智能·ai·开源·ai编程
躺柒1 小时前
读数字时代的网络风险管理:策略、计划与执行04风险指引体系
大数据·网络·信息安全·数字化·网络管理·网络风险管理
橘子131 小时前
MySQL用户管理(十三)
数据库·mysql
Dxy12393102161 小时前
MySQL如何加唯一索引
android·数据库·mysql
我真的是大笨蛋1 小时前
深度解析InnoDB如何保障Buffer与磁盘数据一致性
java·数据库·sql·mysql·性能优化
怣501 小时前
MySQL数据检索入门:从零开始学SELECT查询
数据库·mysql
shengli7221 小时前
机器学习与人工智能
jvm·数据库·python
2301_765703141 小时前
Python迭代器(Iterator)揭秘:for循环背后的故事
jvm·数据库·python
倔强的石头1061 小时前
关键信息基础设施的数据库选型:高可用、全链路安全与平滑替代的技术实践
数据库·安全·金仓数据库