我在 Hive 数据库中有五个表(A、B、C、D、E),我必须根据“id”列的逻辑合并这些表中的数据。
条件是:
Select * from A
UNION
select * from B (except ids not in A)
UNION
select * from C (except ids not in A and B)
UNION
select * from D(except ids not in A,B and C)
UNION
select * from E(except ids not in A,B,C and D)
必须将此数据插入最终表。
一种方法是创建一个目标表 (target) 并将每个 UNION 阶段的数据附加到它,然后使用此表与另一个 UNION 阶段连接。
这将是我的 .hql 文件的一部分:
insert into target
(select * from A
UNION
select B.* from
A
RIGHT OUTER JOIN B
on A.id=B.id
where ISNULL(A.id));
INSERT INTO target
select C.* from
target
RIGHT outer JOIN C
ON target.id=C.id
where ISNULL(target.id);
INSERT INTO target
select D.* from
target
RIGHT OUTER JOIN D
ON target.id=D.id
where ISNULL(target.id);
INSERT INTO target
select E.* from
target
RIGHT OUTER JOIN E
ON target.id=E.id
where ISNULL(target.id);
是否有更好的方法来实现这一点?我想我们无论如何都必须做 多个连接/查找。我期待着实现这一目标的最佳方法 在
1) 使用 Tez 的 Hive
2) Spark-sql
非常感谢
最佳答案
如果 id
在每个表中是唯一的,则可以使用 row_number
代替 rank
。
select *
from (select *
,rank () over
(
partition by id
order by src
) as rnk
from (
select 1 as src,* from a
union all select 2 as src,* from b
union all select 3 as src,* from c
union all select 4 as src,* from d
union all select 5 as src,* from e
) t
) t
where rnk = 1
;
关于sql - 从 Hive 中的多个表中选择增量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326914/