现在的位置: 首页 > 综合 > 正文

Hive文件存储格式

2018年02月08日 ⁄ 综合 ⁄ 共 1870字 ⁄ 字号 评论关闭

Hive文件存储格式包括以下几类:

1.TEXTFILE

2.SEQUENCEFILE

3.RCFILE

4.ORCFILE(0.11以后出现)

其中TEXTFILE是默认格式,建表是不指定默认为这个格式,导入数据时会直接把数据文件拷贝到HDFS上不进行处理。SEQUENCEFILE、RCFILE、RCFILE格式的表不能直接从本地文件导入数据,数据要先导入到TEXTFILE格式的表中,然后再从表中用insert导入到SEQUENCEFILE、RCFILE、ORCFILE等。

前提创建环境:hive0.8

创建一张testfile_table表,格式为textfile:

create table if not exists testfile_table(
          site   string,
          url    string,
          pv     bigint,
          label  string 
) row format delimited fields terminated by '\t' stored as textfile;

加载数据:

load data local inpath '/usr/local/src/weibo.txt' overwrite into table testfile_table;

一、TEXTFILE

默认格式,数据不做压缩,磁盘开销大,数据解析开销大。

可结合Gzip、Bzip2使用(系统自动检查,执行查询时自动解压),但是使用这种方式,hive不会对数据进行切分,从无法对数据进行并行操作。

示例:

//建表
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;

//插入数据前操作
hive> set hive.exec.compress.output=true;
hive> set mapred.output.compress=true;
hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
//插入数据
hive> insert overwrite table textfile_table select * from testfile_table;

二、SEQUENCEFILE

SEQUENCEFILE是hadoop API提供的一种二进制文件支持,其具有使用方便、可分割、可压缩的特点。SEQUENCEFILE支持三种压缩选择:NONE、RECORD、BLOCK。RECORD压缩率低,一般建议使用BLOCK压缩。

示例:

//建表
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;

//插入数据前设置相关属性
hive> set hive.exec.compress.output=true;
hive> set mapred.output.compression.type=BLOCK;

//插入数据
insert overwrite table seqfile_table select * from textfile_table;

三、RCFILE

RCFILE是一种行列存储相结合方式。首先,其将数据按行分块,保证同一个record在一个块上,避免读一个记录需要读取多个block块。其次块数据列式存储,有利于数据压缩和快速的列存取。

示例:

//建表
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;  
//插入数据
insert overwrite table rcfile_table select * from testfile_table;

抱歉!评论已关闭.