获取 Hive 中两个表的完整数据视图?

Get full data view for two tables in Hive?

我在 Hive 中有两个表(archnoarch),结构如下:

Table1Arch Table2NoArch
tr_id tr_id
res_id res_id
info_json info_json
created_at
updated_at

我需要查看完整数据 arch + noarch 并在 res_id 前加入他们。

我尝试对左连接进行不同的变体,但我要么从 arch 得到结果,而从 noarch 得不到任何结果,反之亦然。我想我应该使用 union all,但很难正确地写它。

你能帮我查询一下吗?

编辑:

我希望在搜索特定 res_id 时获得这两个表的统一视图。

假设我有以下数据:

Entry1Arch Entry2Arch
1 2
111 222
{"something 1"} {"something 2"}
Entry3NoArch Entry4NoArch
3 4
333 444
{"something 3"} {"something 4"}
2021-10-03 21:01:44.0 2021-10-04 21:02:43.0
2021-10-03 21:01:44.0 2021-10-04 21:02:43.0

最终目标是从两个表中获取完整数据:111 + 222 + 333 + 444)。

您可以使用 UNION ALL:

select tr_id, res_id, info_json, created_at, updated_at, src
from
(select tr_id, res_id, info_json, created_at, updated_at, 'NoArch' as src 
  from Table2NoArch

union all

select tr_id, res_id, info_json, null created_at, null updated_at, 'Arch' as src 
  from Table1Arch
)u
where res_id in (111,333,444)
一个Table1Arch中没有

created_at和updated_at,选择了NULL,可以用current_timestamp或current_date代替。

添加了 src 列,这样您就可以轻松找到数据的来源。