Hive文件格式(表STORE AS 的四种类型)

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

1、TEXTFILE

2、SEQUENCEFILE

3、RCFILE

4、ORCFILE(0.11以后出现)

其中TEXTFILE为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理;

SEQUENCEFILE,RCFILE,ORCFILE格式的表不能直接从本地文件导入数据,数据要先导入到textfile格式的表中, 然后再从表中用insert导入SequenceFile,RCFile,ORCFile表中。

前提创建环境:

hive 0.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 '/app/weibo.txt' overwrite into table textfile_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;
插入数据操作:
set hive.exec.compress.output=true;  
set mapred.output.compress=true;  
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;  
insert overwrite table textfile_table select * from textfile_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;
插入数据操作:
set hive.exec.compress.output=true;  
set mapred.output.compress=true;  
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;  
SET mapred.output.compression.type=BLOCK;
insert overwrite table seqfile_table select * from textfile_table;  
复制代码

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

复制代码
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;  
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;  
insert overwrite table rcfile_table select * from textfile_table;
复制代码

四、ORCFILE()
五、再看TEXTFILE、SEQUENCEFILE、RCFILE三种文件的存储情况:

复制代码
[hadoop@node3 ~]$ hadoop dfs -dus /user/hive/warehouse/*
hdfs://node1:19000/user/hive/warehouse/hbase_table_1    0
hdfs://node1:19000/user/hive/warehouse/hbase_table_2    0
hdfs://node1:19000/user/hive/warehouse/orcfile_table    0
hdfs://node1:19000/user/hive/warehouse/rcfile_table    102638073
hdfs://node1:19000/user/hive/warehouse/seqfile_table   112497695
hdfs://node1:19000/user/hive/warehouse/testfile_table  536799616
hdfs://node1:19000/user/hive/warehouse/textfile_table  107308067
[hadoop@node3 ~]$ hadoop dfs -ls /user/hive/warehouse/*/
-rw-r--r--   2 hadoop supergroup   51328177 2014-03-20 00:42 /user/hive/warehouse/rcfile_table/000000_0
-rw-r--r--   2 hadoop supergroup   51309896 2014-03-20 00:43 /user/hive/warehouse/rcfile_table/000001_0
-rw-r--r--   2 hadoop supergroup   56263711 2014-03-20 01:20 /user/hive/warehouse/seqfile_table/000000_0
-rw-r--r--   2 hadoop supergroup   56233984 2014-03-20 01:21 /user/hive/warehouse/seqfile_table/000001_0
-rw-r--r--   2 hadoop supergroup  536799616 2014-03-19 23:15 /user/hive/warehouse/testfile_table/weibo.txt
-rw-r--r--   2 hadoop supergroup   53659758 2014-03-19 23:24 /user/hive/warehouse/textfile_table/000000_0.gz
-rw-r--r--   2 hadoop supergroup   53648309 2014-03-19 23:26 /user/hive/warehouse/textfile_table/000001_1.gz
复制代码

总结:
相比TEXTFILE和SEQUENCEFILE,RCFILE由于列式存储方式,数据加载时性能消耗较大,但是具有较好的压缩比和查询响应。数据仓库的特点是一次写入、多次读取,因此,整体来看,RCFILE相比其余两种格式具有较明显的优势。

  • 3
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 是的,创建时需要指定文件格式。在Hive中,可以通过使用“STORED AS”子句来指定文件格式,例如: CREATE TABLE my_table ( col1 INT, col2 STRING ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE; 上述示例创建了一个名为“my_table”的,并将其存储为文本文件格式。在创建时,还可以使用其他文件格式,如ORC、Parquet、Avro等。 ### 回答2: 在Hive中创建时,可以选择是否指定文件格式Hive支持多种文件格式,如文本格式(TextFile)、序列文件格式(SequenceFile)、行列式存储文件格式(RCFile)等。默认情况下,如果不指定文件格式,则使用文本格式作为默认格式。 指定文件格式有一些好处。首先,不同的文件格式适用于不同的场景和需求。例如,文本格式适用于简单的文本数据,而序列文件格式适用于需要高效查询和压缩的大规模数据集。因此,在创建时选择合适的文件格式可以提高查询性能和节省存储空间。 其次,指定文件格式还可以方便数据的导入和导出。如果数据源是非文本格式,如压缩文件或其他数据库中的,使用相应的文件格式可以直接将数据导入到Hive中。同时,指定文件格式也可以灵活地将Hive的数据导出到其他系统或平台中。 总而言之,在Hive中创建时可以选择是否指定文件格式,根据实际需求选择适合的格式可以提高查询性能、节省存储空间,并方便数据的导入和导出。 ### 回答3: 在Hive中创建时,可以选择是否指定文件格式。如果没有明确指定文件格式Hive会根据默认的设置来处理数据文件。 Hive默认情况下支持多种文件格式,如文本格式(TextFile)、序列文件格式(SequenceFile)、Parquet格式、ORC格式等。因此,在创建时,可以根据实际需求选择合适的文件格式。 如果想要指定文件格式,可以使用“STORED AS”关键字,并在后面加上所需的文件格式。例如,可以使用以下语句在Hive中创建一个存储为Parquet格式的: CREATE TABLE table_name ( column1 data_type, column2 data_type ) STORED AS PARQUET; 需要注意的是,指定文件格式不仅影响数据的存储方式,还会影响到数据的查询性能。不同的文件格式在数据的压缩、存储效率和查询速度上有所差异。因此,在选择文件格式时,需要考虑到数据的读写需求以及系统的性能要求。 总之,在Hive中创建时,可以选择是否指定文件格式,可以根据实际需求和性能要求来决定是否需要指定文件格式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值