sql - 从 Hive 中的多个表中选择增量数据

标签 sql apache hadoop hive apache-spark-sql

我在 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/

相关文章:

sql - PostgreSQL 错误 : query has no destination for result data

java - java中的非唯一表别名错误

sql - 使用字符串输入在表中插入多个值

Apache mod_rewrite 接受语言子域重定向

php - 使用 IIS 实现搜索引擎友好的 URL

限制为 0 的 SQL 查询

php - 是什么杀死了这个进程以及如何阻止它这样做

python - 监控节点集群

hadoop - hadoop中的NameNode未运行并且无法访问(http://localhost:50030)

hadoop - pig -获取具有无效日期格式的所有记录