【问题标题】:Get full data view for two tables in Hive?获取 Hive 中两个表的完整数据视图?
【发布时间】:2022-01-01 03:20:55
【问题描述】:

我在 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_ids 时,我想获得这两个表的统一视图。

假设我有以下数据:

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)。

【问题讨论】:

  • 你需要什么结果?请提供两个表格中的输入数据示例和预期结果
  • @leftjoin 已更新,如果有帮助请告诉我
  • 如果两个表包含相同的 res_id 怎么办?
  • 啊,谢谢,我的错。那么查询不正确。我基本上想要这两个表的并集

标签: sql hive hiveql hue


【解决方案1】:

您可以使用 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栏目,方便查找数据来源。

【讨论】:

  • ..具体来说,如果我想要两个表的一些特定 ID?
  • @Stranger95 您可以将联合放入子查询或视图中,然后使用过滤器。更新了答案。谓词将被推送到联合中的每个查询中。
  • 非常感谢。你也知道如何应对FAILED: SemanticException Schema of both sides of union should match吗?
  • @Stranger95 两个查询应该具有相同的列数和相同的类型。如果它们的类型不同,您可以转换列。例如 cast(null as timestamp) as created_at .... 匹配来自其他查询的时间戳类型(如果它是时间戳)
  • @Stranger95 检查所有列类型并进行相应的转换,空值也应该转换为相同的类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多