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
3)实操一下:
首先将一个18.1M的数据上传至 /home/hivedata/,这个文件叫做stocks.log
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;