hive中如何将rcfile,textfile,sequencefile,以及自定义存储格式的数据相互转换

        hive中常见的文件存储格式也就3种,textfile,sequencefile,rcfile.实际开发中,很多公司都会采用自定义的存储格式来实现数据的特定存储。一方面是为了数据安全,另一方面是根据自身情况实现数据存储的效益最大化。

       1.使用textfile存储格式创建表

create table fdm_sor.saveas_textfile(
id int ,
name string )
stored as textfile
       2.使用sequencefile存储格式创建表
 
  create table fdm_sor.saveas_sequencefile(
  id int ,
  name string )
  stored as sequencefile

        3.使用rcfile存储格式创建表      

create table fdm_sor.saveas_rcfile(
  id int ,
  name string )
  stored as rcfile
        4.使用自定义格式创建表
add jar /home/bigdata/software/hive/ext-lib/suning-hive-inputformat.jar; 
create table fdm_sor.saveas_suningformat(
id int ,
name string )
STORED AS
 INPUTFORMAT
'com.suning.hive.input.CommonTextInputFormat' 
OUTPUTFORMAT 
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';

       像我们公司,大数据中心分析的数据一般都是从业务的数据库,如mysql,oracle,db2等里面直接抽数的,所以在hive的SSA层存储数据的时候,默认都是用自定义存储格式和自定义的Serde序列化。在sor处理层所有的数据存储都是默认使用rcfile文件存储格式。在dm层,一般都是使用textfile存储数据。sequencefile基本不怎么用。那不同的层面数据该如何转换呢。其实在hive中不同存储格式转换很简单,只需要使用insert ....select 即可
       1.将textfile格式的数据如何转换成rcfile存储

hive (fdm_sor)> insert overwrite table saveas_rcfile
              > select id ,name from  saveas_textfile;

       2.同理将textfile存储格式的数据转换成sequencefile

hive (fdm_sor)> insert overwrite table saveas_sequencefile
              > select id ,name from  saveas_textfile; 

      3.总之都可以按照上面这种方式将不同存储格式的数据互相转换。

     注意:如果往hive中导入的数据源不是数据库(也就是说不是通过insert ...select形式),而是外部文件的形式,比如通过load data ....加载的方式,那么要注意建表时存储格式必须和提供的文件存储格式一致。否则数据导入失败,这种情况下不能将三种存储格式任意转换使用。比如,将提供的文本文件,用load导入到stored  as rcfile表中,则报错,文件格式不匹配。这种情况,文本文件只能load进stored as textfile表中,然后再通过上面的insert ....select形式进行其他格式的转换。

           此外,如何通过load 形式往表中导入数据时,除了定义对应的存储格式,还要定义数据记录解析格式,比如文本文件中数据以‘||’来分割的,那么创建表时要指定row format delimited fields terminated by '||',而不能使用默认的serde进行解析,否则该条记录数据都会加载到一个列中。

      具体三者之间的关系和区别使用,参考我的其他博客。

猜你喜欢

转载自blog.csdn.net/qq_26442553/article/details/80379872
今日推荐