首页 技术 正文
技术 2022年11月14日
0 收藏 346 点赞 2,592 浏览 2939 个字

TextFile

  • Hive数据表的默认格式,存储方式:行存储。
  • 可使用Gzip,Bzip2等压缩算法压缩,压缩后的文件不支持split
  • 但在反序列化过程中,必须逐个字符判断是不是分隔符和行结束符,因此反序列化开销会比SequenceFile高几十倍。
--创建数据表:
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; --指定输出的压缩格式为Gzip
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
insert overwrite table textfile_table select * from T_Name;

Hive压缩格式

SequenceFile

  • Hadoop API提供的一种二进制文件,以<key,value>的形式序列化到文件中。存储方式:行存储。
  • 支持三种压缩选择:NONE,RECORD,BLOCK。Record压缩率低,一般建议使用BLOCK压缩。
  • 优势是文件和hadoop api中的MapFile是相互兼容的
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; --指定输出压缩格式为Gzip
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
SET mapred.output.compression.type=BLOCK; --指定为Block
insert overwrite table seqfile_table select * from T_Name;

Hive压缩格式

RCFile

存储方式:数据按行分块,每块按列存储。结合了行存储和列存储的优点:

  • 首先,RCFile 保证同一行的数据位于同一节点,因此元组重构的开销很低
  • 其次,像列存储一样,RCFile 能够利用列维度的数据压缩,并且能跳过不必要的列读取

RCFile的一个行组包括三个部分:

  1. 第一部分是行组头部的【同步标识】,主要用于分隔 hdfs 块中的两个连续行组
  2. 第二部分是行组的【元数据头部】,用于存储行组单元的信息,包括行组中的记录数、每个列的字节数、列中每个域的字节数
  3. 第三部分是【表格数据段】,即实际的列存储数据。在该部分中,同一列的所有域顺序存储。
     从图可以看出,首先存储了列 A 的所有域,然后存储列 B 的所有域等。

Hive压缩格式

数据追加:RCFile 不支持任意方式的数据写操作,仅提供一种追加接口,这是因为底层的 HDFS当前仅仅支持数据追加写文件尾部。 
行组大小:行组变大有助于提高数据压缩的效率,但是可能会损害数据的读取性能,因为这样增加了 Lazy 解压性能的消耗。而且行组变大会占用更多的内存,这会影响并发执行的其他MR作业。 考虑到存储空间和查询效率两个方面,Facebook 选择 4MB 作为默认的行组大小,当然也允许用户自行选择参数进行配置。

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 T_Name;

Hive压缩格式

ORCFile

  • 存储方式:数据按行分块 每块按照列存储
  • 压缩快 快速列存取
  • 效率比rcfile高,是rcfile的改良版本

自定义格式

  • 用户可以通过实现inputformat和 outputformat来自定义输入输出格式。
hive>  create table myfile_table(str STRING)
> stored as
> inputformat 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextInputFormat'
> outputformat 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextOutputFormat';
OK
Time taken: 0.399 seconds
hive> load data local inpath '/root/hive/myfile_table'
> overwrite into table myfile_table;--加载数据
hive> dfs -text /user/hive/warehouse/myfile_table/myfile_table;--数据文件内容,编码后的格式
aGVsbG8saGl2ZQ==
aGVsbG8sd29ybGQ=
aGVsbG8saGFkb29w
hive> select * from myfile_table;--使用自定义格式进行解码
OK
hello,hive
hello,world
hello,hadoop
Time taken: 0.117 seconds, Fetched: 3 row(s)

总结:

数据仓库的特点:一次写入、多次读取,因此,整体来看,ORCFile相比其他格式具有较明显的优势。

  • TextFile 默认格式,加载速度最快,可以采用Gzip、bzip2等进行压缩,压缩后的文件无法split,即并行处理
  • SequenceFile 压缩率最低,查询速度一般,三种压缩格式NONE,RECORD,BLOCK
  • RCfile 压缩率最高,查询速度最快,数据加载最慢。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291