使用动态分区的方式为Hive中的分区表加载数据

1.创建临时表,为临时表添加数据

Drop table if exists test.tmp;
create table test.tmp(
 shopid int,
    userid int,
    areaid int,
    shopname string,
    shoplevel tinyint,
    status tinyint,
    createtime string,
    modifytime string,
    dt string
)
row format delimited fields terminated by ',';
load data local inpath 'tmp.dat' into table test.tmp;

tmp.dat

105307,78921,100049,如山xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105308,78921,100236,美丽xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105309,78921,100063,Juxxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105310,78921,100235,兰思xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105311,78921,100016,LExxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105312,78921,100325,美乐xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105313,78921,100285,奥克xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105314,78921,100116,贝加xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105315,78921,100036,Caxxx授权店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01
105316,78921,100143,凯萨xxx旗舰店,1,1,2020-06-28,2020-07-01 13:22:22,2020-07-01

2.创建分区表

Drop table if exists test.ods_trade_shops;
create table test.ods_trade_shops(
 shopid int,
    userid int,
    areaid int,
    shopname string,
    shoplevel tinyint,
    status tinyint,
    createtime string,
    modifytime string
)
partitioned by(dt string)
row format delimited fields terminated by ',';

3.在hive命令行设置动态分区参数

set hive.exec.dynamic.partition=true;   --此参数hive0.9.0版本开始默认就为true,使用hive0.9.0及以上版本可以不用设置

set hive.exec.dynamic.partition.mode=nonstrict;

4.为分区表动态加载数据

insert into table test.ods_trade_shops
partition(dt)
select * from test.tmp;

猜你喜欢

转载自blog.csdn.net/abc5254065/article/details/112948224
今日推荐