Hive导入数据

2017-12-11  本文已影响0人  编程回忆录
load data local inpath '/Users/wesley/apps/data/hive_learning_data/employees/employees.txt' overwrite into table employees partition (country='US',state='CA');
insert overwrite table employees
partition (country='US',state='CA')
select *
from staged_employees t2
where t2.country='US' and t2.state='CA';

如果staged_employees表有多个state需要插入到employees表的对应state分区中,我们可以这样写:

from staged_employees t2
insert overwrite table employees
          partition (country='US',state='CA')
select * where t2.country='US' and t2.state='CA'
insert overwrite table employees
          partition (country='US',state='OR')
select * where t2.country='US' and t2.state='OR'
insert overwrite table employees
          partition (country='US',state='IL')
select * where t2.country='US' and t2.state='IL'

通过上面的sql我们可以一次插入多个分区,但是如果需要插入的分区太多,写起来的sql太长太繁琐,这时候我们可以利用动态分区来插入:

insert overwrite table employees
          partition (country,state)
select name,salary,subordinates,deductions,address,country,state
from staged_employees

注意分区字段country、state必须写在select最后

create table employees_us
as
select name,salary,subordinates,deductions,address,country,state
from staged_employees
where country='US'
上一篇 下一篇

猜你喜欢

热点阅读