--原数据
[root@master]# vi a_base_tab.txt
1001,sunwukong,assassin,male
1002,HANXIN,assassin,male
[root@master]# vi i_base_tab.txt
1002,hanxin,assassin,male
1003,zhaoyun,assassin,male
--上传数据
[root@master]# hadoop fs -put a_base_tab.txt /user/hive/external/a_base_tab
[root@master]# hadoop fs -put i_base_tab.txt /user/hive/external/i_base_tab
--创建外部表 a_base_tab
create external table a_base_tab
(id string,name string,job string,sex string)
row format delimited fields terminated by ','
location '/user/hive/external/a_base_tab';
--创建外部表 i_base_tab
create external table i_base_tab
(id string,name string,job string,sex string)
row format delimited fields terminated by ','
location '/user/hive/external/i_base_tab';
--测试 full outer 语法
select a.*,i.* from a_base_tab a full outer join i_base_tab i on a.id=i.id;
1001 sunwukong assassin male NULL NULL NULL NULL
1002 hanxin assassin male 1002 hanxin assassin male
1003 zhaoyun assassin male 1003 zhaoyun assassin male
--测试 coalesce 语法
select coalesce(a.id,i.id) from a_base_tab a full outer join i_base_tab i on a.id=i.id;
1001
1002
1003
--测试 if 语法
select if(i.id is null,a.name,i.name) from a_base_tab a full outer join i_base_tab i on a.id=i.id;
sunwukong
hanxin
zhaoyun
--最后综合起来,得到我们想要的HQL语句:
insert overwrite table a_base_tab
select coalesce(a.id,i.id) as id,
if(i.id is null,a.name,i.name) as name,
if(i.id is null,a.job,i.job) as job,
if(i.id is null,a.sex,i.sex) as sex
from a_base_tab a full outer join i_base_tab i on a.id=i.id;
1001 sunwukong assassin male
1002 hanxin assassin male
1003 zhaoyun assassin male