hive分区表批量入库

Hive 跟 传统数据库不一样,没有索引的概念, 只有一个 分区列 的概念。 分区是一个增加快速查找的有效解决方案,但同时又带来一个新问题,如何同时插入多个分区字段的数据呢?  方法如下:

 

# 创建分区表
CREATE TABLE temp.atest_subsir_ab_userid ( 
    user_id string
) partitioned by (dt string, type string);
# 创建临时表,后面会将其数据插入到 分区表 内
CREATE TABLE temp.aatest_tmp 
(
    userid string, 
    type string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

# 临时表数据插入
INSERT OVERWRITE TABLE temp.aatest_tmp 
select alls.userid, alls.type from ( 
    select userid, 'a' as type from temp.atest_push_ab_userid_a 
    union all 
    select userid, 'b' as type from temp.atest_push_ab_userid_b 
) alls ;
# 分区表 批量插入多分区数据
set hive.exec.dynamic.partition=true; # 允许动态分区
INSERT OVERWRITE TABLE temp.atest_subsir_ab_userid PARTITION (mode='20141116', type) 
select userid, type from temp.aatest_tmp  ;

 

就这样简单插入,数据就OK啦。。 就可以通过分区访问啦。。  简单吧!!!

 

posted @ 2014-11-18 12:35  subsir  阅读(950)  评论(0编辑  收藏  举报