zoukankan      html  css  js  c++  java
  • Hadoop Hive概念学习系列之hive的数据压缩(七)

    Hive文件存储格式包括以下几类:
    1、TEXTFILE
    2、SEQUENCEFILE
    3、RCFILE
    4、ORCFILE
      其中TEXTFILE为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理。
      SEQUENCEFILE,RCFILE,ORCFILE格式的表不能直接从本地文件导入数据,数据要先导入到textfile格式的表中, 然后再从表中用insert导入SequenceFile,RCFile,ORCFile表中。

    更多用法,一定要去看官网啊!!!
      https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

    一、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 '	'
    stored as textfile;


      插入数据操作:

    Hive> 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 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 '	'
    stored as sequencefile;


      插入数据操作:

    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> SET mapred.output.compression.type=BLOCK;
    Hive> 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 '	'
    stored as rcfile;


      插入数据操作:

    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 rcfile_table select * from textfile_table;


    四、ORCFILE()
      以后补充


    五、再看TEXTFILE、SEQUENCEFILE、RCFILE三种文件的存储情况:
    [hadoop@master ~]$ hadoop dfs -dus /user/Hive/warehouse/*
    hdfs://master :9000/user/Hive/warehouse/hbase_table_1 0
    hdfs://master :9000/user/Hive/warehouse/hbase_table_2 0
    hdfs://master :9000/user/Hive/warehouse/orcfile_table 0
    hdfs://master :9000/user/Hive/warehouse/rcfile_table 102638073
    hdfs://master :9000/user/Hive/warehouse/seqfile_table 112497695
    hdfs://master :9000/user/Hive/warehouse/testfile_table 536799616
    hdfs://master :9000/user/Hive/warehouse/textfile_table 107308067
    [hadoop@singlehadoop ~]$ 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相比其余两种格式具有较明显的优势。

    以下,本文转自于。http://blog.csdn.net/cnbird2008/article/details/9182869

    Hive数据压缩

    本文介绍Hadoop系统中Hive数据压缩方案的比较结果及具体压缩方法。

    一、压缩方案比较

    关于Hadoop HDFS文件的压缩格式选择,我们通过多个真实的Track数据做测试,得出结论如下:

    1.  系统的默认压缩编码方式 DefaultCodec 无论在压缩性能上还是压缩比上,都优于GZIP 压缩编码。这一点与网上的一些观点不大一致,网上不少人认为GZIP的压缩比要高一些,估计和Cloudera的封装及我们Track的数据类型有关。

    2.  Hive文件的RCFile 的在压缩比,压缩效率,及查询效率上都优于SEQENCE FILE (包括RECORD, BLOCK 级别) 。

    3.  所有压缩文件均可以正常解压为TEXT 文件,但比原始文件略大,可能是行列重组造成的。

    关于压缩文件对于其他组件是适用性如下:

    1.  Pig 不支持任何形式的压缩文件。

    2.  Impala 目前支持SequenceFile的压缩格式,但还不支持RCFile的压缩格式。

    综上所述

      从压缩及查询的空间和时间性能上来说,DefaultCodeC + RCFile的压缩方式均为最优,但使用该方式,会使得Pig 和Impala 无法使用(Impala的不兼容不确定是否是暂时的)。

      而DefaultCodeC+ SequenceFile 在压缩比,查询性能上略差于RCFile (压缩比约 6:5), 但可以支持 Impala实时查询。

    推荐方案

     采用RCFile 方式压缩历史数据。FackBook全部hive表都用RCFile存数据。

    二、局部压缩方法

    只需要两步:

    1.      创建表时指定压缩方式,默认不压缩,以下为示例:

    create external table track_hist(

    id bigint, url string, referer string, keyword string, type int, gu_idstring,

    …/*此处省略中间部分字段*/ …, string,ext_field10 string)

    partitioned by (ds string) stored as RCFile location '/data/share/track_histk' ;

    2.  插入数据是设定立即压缩

    SET hive.exec.compress.output=true;

    insert overwrite table track_histpartition(ds='2013-01-01')

    select id,url, …/*此处省略中间部分字段*/ …, ext_field10 fromtrackinfo

    where ds='2013-01-01';

    三、全局方式,修改属性文件

    在hive-site.xml中设置:

    <property>

     <name>hive.default.fileformat</name>

     <value>RCFile</value>

     <description>Default file format for CREATE TABLE statement.Options are TextFile and SequenceFile. Users can explicitly say CREAT

    E TABLE ... STORED AS&lt;TEXTFILE|SEQUENCEFILE&gt; to override</description>

    </property>

    <property>

     <name>hive.exec.compress.output</name>

     <value>true</value>

     <description> This controls whether the final outputs of a query(to a local/hdfs file or a hive table) is compressed. The compres

    sion codec and other options are determinedfrom hadoop config variables mapred.output.compress* </description>

    四、注意事项

    1、Map阶段输出不进行压缩

    2、对输出文本进行处理时不压缩

  • 相关阅读:
    08-30 作业整理
    JS题目整理
    08-20作业整理
    CSS3知识总结
    CSS样式表知识总结
    html标签知识总结
    Dice (III) 概率dp
    Island of Survival 概率
    How far away ?
    Hdu 2874 Connections between cities
  • 原文地址:https://www.cnblogs.com/zlslch/p/6103760.html
Copyright © 2011-2022 走看看