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;
比较一下上述五个查询所需要的时间

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

相关推荐
爱吃烤鸡翅的酸菜鱼5 分钟前
IDEA高效开发:Database Navigator插件安装与核心使用指南
java·开发语言·数据库·编辑器·intellij-idea·database
超奇电子10 分钟前
阿里云OSS预签名URL上传与临时凭证上传的技术对比分析
数据库·阿里云·云计算
神仙别闹22 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
m0_6530313638 分钟前
PostgreSQL技术大讲堂 - 第97讲:PG数据库编码和区域(locale)答疑解惑
数据库·postgresql
计艺回忆路1 小时前
Hive自定义函数(UDF)开发和应用流程
hive·自定义函数·udf
会编程的林俊杰1 小时前
MySQL中的锁有哪些
数据库·mysql
cts6181 小时前
Milvus分布式数据库工作职责
数据库·分布式·milvus
周胡杰1 小时前
鸿蒙加载预置数据库-关系型数据库-如何读取本地/预制数据库
数据库·华为·harmonyos·鸿蒙
布朗克1681 小时前
java常见的jvm内存分析工具
java·jvm·数据库
胡八一1 小时前
SQLite / LiteDB 单文件数据库为何“清空表后仍占几 GB”?——原理解析与空间回收实战
jvm·数据库·sqlite