将Hive数据库存储格式转换为orc

Hive的存储格式

  • textfile

    hive的默认存储格式
    存储方式:行存储
    磁盘开销大 数据解析开销大
    压缩的text文件 hive无法进行合并和拆分

  • SequenceFile

    二进制文件以key,value的形式序列化到文件中
    存储方式:行存储
    可分割 压缩
    一般选择block压缩
    优势是文件和hadoop api中的mapfile是相互兼容的

  • rcfile

    存储方式:数据按行分块 每块按照列存储
    压缩快 快速列存取
    读记录尽量涉及到的block最少
    读取需要的列只需要读取每个row group 的头部定义。
    读取全量数据的操作 性能可能比sequencefile没有明显的优势

  • orc

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

  • 自定义格式

    用户可以通过实现inputformat和 outputformat来自定义输入输出格式

Hive导入数据的几种方式

  • 从本地文件系统中导入
load data local inpath 'customer .data' into table customer;
  • 从HDFS上导入
load data inpath '/hive/customer .data' into table customer;
  • 从别的表中查询出相应的数据导入
insert into table customer select * from customer_tmp;

将存储格式转换为orc

因为textfile类型的数据不能直接保存到orc类型的表中,根据上面的几种导入数据的方式我们做一下转换就可以了,先导入到一个textfile类型的表中然后在通过查询导入到另外一个表就可以了

示例:

use tpcds_orc;
drop table if exists customer_tmp;
create table customer_tmp
(
    c_customer_sk             int                           ,
    c_customer_id             char(16)                      ,
    c_current_cdemo_sk        int                           ,
    c_current_hdemo_sk        int                           ,
    c_current_addr_sk         int                           ,
    c_first_shipto_date_sk    int                           ,
    c_first_sales_date_sk     int                           ,
    c_salutation              char(10)                      ,
    c_first_name              char(20)                      ,
    c_last_name               char(30)                      ,
    c_preferred_cust_flag     char(1)                       ,
    c_birth_day               int                           ,
    c_birth_month             int                           ,
    c_birth_year              int                           ,
    c_birth_country           varchar(20)                   ,
    c_login                   char(13)                      ,
    c_email_address           char(50)                      ,
    c_last_review_date        char(10)
)
row format delimited fields terminated by '|';
load data local inpath '/data1/tpcds/data100/customer.dat' into table customer_tmp;
drop table if exists customer;
create table customer
(
    c_customer_sk             int                           ,
    c_customer_id             char(16)                      ,
    c_current_cdemo_sk        int                           ,
    c_current_hdemo_sk        int                           ,
    c_current_addr_sk         int                           ,
    c_first_shipto_date_sk    int                           ,
    c_first_sales_date_sk     int                           ,
    c_salutation              char(10)                      ,
    c_first_name              char(20)                      ,
    c_last_name               char(30)                      ,
    c_preferred_cust_flag     char(1)                       ,
    c_birth_day               int                           ,
    c_birth_month             int                           ,
    c_birth_year              int                           ,
    c_birth_country           varchar(20)                   ,
    c_login                   char(13)                      ,
    c_email_address           char(50)                      ,
    c_last_review_date        char(10)
)
row format delimited fields terminated by '|'
stored as orc ;
insert into table customer select * from customer_tmp;
drop table customer_tmp;
发布了14 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/u010942041/article/details/62885962