Hive/Impala - 在层次结构中查找结束子节点 table
Hive/Impala - Find End Child nodes in Hierarchy Structure table
我有一个场景,从具有 parent_node_id 和 child_node_id 的层次结构 table 中找到最低级别的子节点,如下所示。
来源 table 在 Hive 和 Impala 数据库中。
请建议 hive/impala 查询以找出源 table 中每个父节点的最低级别子节点。
我尝试在 Impala 中使用 CTE 递归查询,但我猜它不受支持。
提前致谢!
来源Table:
+-------------+--------------+
|child_node_id|parent_node_id|
+-------------+--------------+
| C1 | P1 |
+-------------+--------------+
| C2 | P2 |
+-------------+--------------+
| C11 | C1 |
+-------------+--------------+
| C12 | C11 |
+-------------+--------------+
| 123 | C12 |
+-------------+--------------+
预期输出:
+-------------+--------------+
|parent_node |lowest_l_child|
+-------------+--------------+
| P1 | 123 |
+-------------+--------------+
| P2 | C2 |
+-------------+--------------+
| C1 | 123 |
+-------------+--------------+
| C11 | 123 |
+-------------+--------------+
| C12 | 123 |
+-------------+--------------+
因为 hive 不支持递归 CTE 查询。
一个选项请参考[https://blog.pythian.com/recursion-in-hive/][1]
其他选项是使用 shell 脚本循环和查询以找到所有 parents.
的最低 child
步骤 -
1> 初始化(一次运行)
create temporary table hier_temp as select * from Hier;
create table if not exists res as select * from hier where false;
2) 查询以找到最低级别 child
insert into table res
select
H1.parent, H1.Child
from hier_temp H1 left outer join hier_temp H2
on H1.Child=H2.Parent where H2.Child is null;
3) 用 child
的下一级覆盖温度 table
insert overwrite table hier_temp
select
H1.Parent Parent, coalesce(H3.child, H2.child) as child
from hier_temp H1 left outer join hier_temp H2 on H1.Child=H2.Parent
left outer join res H3 on H2.child=H3.parent
where H2.Child is not null;
创建一个 shell 脚本,它将在循环中按顺序执行步骤 2 和 3(带有 break 和 continue 的条件语句将完成这项工作)直到我们在 hier_temp 中没有任何数据table.
以下是给定测试数据的 res 和 hier_temp table 的结果。
hive> select * from res;
OK
Time taken: 0.131 seconds
hive> select * from hier_temp;
OK
C1 C11
C11 C12
C12 123
P1 C1
P2 C2
Time taken: 0.108 seconds, Fetched: 5 row(s)
第 2 步和第 3 步中提到的查询在循环 1 后的结果
hive> select * from res;
OK
C12 123
P2 C2
Time taken: 0.137 seconds, Fetched: 2 row(s)
hive> select * from hier_temp;
OK
P1 C11
C1 123
C11 123
Time taken: 0.155 seconds, Fetched: 3 row(s)
第 2 步和第 3 步中提到的查询的循环 2 后的结果
hive> select * from res;
OK
C12 123
P2 C2
C1 123
C11 123
Time taken: 0.11 seconds, Fetched: 4 row(s)
hive> select * from hier_temp;
OK
P1 123
Time taken: 0.111 seconds, Fetched: 1 row(s)
第 2 步和第 3 步中提到的查询的结果循环 3
hive> select * from res;
OK
P1 123
C12 123
P2 C2
C1 123
C11 123
Time taken: 0.115 seconds, Fetched: 5 row(s)
hive> select * from hier_temp;
OK
Time taken: 0.16 seconds
这会得到您想要的结果,但您可能需要考虑执行时间。
希望对您有所帮助
我有一个场景,从具有 parent_node_id 和 child_node_id 的层次结构 table 中找到最低级别的子节点,如下所示。 来源 table 在 Hive 和 Impala 数据库中。 请建议 hive/impala 查询以找出源 table 中每个父节点的最低级别子节点。
我尝试在 Impala 中使用 CTE 递归查询,但我猜它不受支持。
提前致谢!
来源Table:
+-------------+--------------+
|child_node_id|parent_node_id|
+-------------+--------------+
| C1 | P1 |
+-------------+--------------+
| C2 | P2 |
+-------------+--------------+
| C11 | C1 |
+-------------+--------------+
| C12 | C11 |
+-------------+--------------+
| 123 | C12 |
+-------------+--------------+
预期输出:
+-------------+--------------+
|parent_node |lowest_l_child|
+-------------+--------------+
| P1 | 123 |
+-------------+--------------+
| P2 | C2 |
+-------------+--------------+
| C1 | 123 |
+-------------+--------------+
| C11 | 123 |
+-------------+--------------+
| C12 | 123 |
+-------------+--------------+
因为 hive 不支持递归 CTE 查询。
一个选项请参考[https://blog.pythian.com/recursion-in-hive/][1]
其他选项是使用 shell 脚本循环和查询以找到所有 parents.
的最低 child步骤 - 1> 初始化(一次运行)
create temporary table hier_temp as select * from Hier;
create table if not exists res as select * from hier where false;
2) 查询以找到最低级别 child
insert into table res
select
H1.parent, H1.Child
from hier_temp H1 left outer join hier_temp H2
on H1.Child=H2.Parent where H2.Child is null;
3) 用 child
的下一级覆盖温度 tableinsert overwrite table hier_temp
select
H1.Parent Parent, coalesce(H3.child, H2.child) as child
from hier_temp H1 left outer join hier_temp H2 on H1.Child=H2.Parent
left outer join res H3 on H2.child=H3.parent
where H2.Child is not null;
创建一个 shell 脚本,它将在循环中按顺序执行步骤 2 和 3(带有 break 和 continue 的条件语句将完成这项工作)直到我们在 hier_temp 中没有任何数据table.
以下是给定测试数据的 res 和 hier_temp table 的结果。
hive> select * from res;
OK
Time taken: 0.131 seconds
hive> select * from hier_temp;
OK
C1 C11
C11 C12
C12 123
P1 C1
P2 C2
Time taken: 0.108 seconds, Fetched: 5 row(s)
第 2 步和第 3 步中提到的查询在循环 1 后的结果
hive> select * from res;
OK
C12 123
P2 C2
Time taken: 0.137 seconds, Fetched: 2 row(s)
hive> select * from hier_temp;
OK
P1 C11
C1 123
C11 123
Time taken: 0.155 seconds, Fetched: 3 row(s)
第 2 步和第 3 步中提到的查询的循环 2 后的结果
hive> select * from res;
OK
C12 123
P2 C2
C1 123
C11 123
Time taken: 0.11 seconds, Fetched: 4 row(s)
hive> select * from hier_temp;
OK
P1 123
Time taken: 0.111 seconds, Fetched: 1 row(s)
第 2 步和第 3 步中提到的查询的结果循环 3
hive> select * from res;
OK
P1 123
C12 123
P2 C2
C1 123
C11 123
Time taken: 0.115 seconds, Fetched: 5 row(s)
hive> select * from hier_temp;
OK
Time taken: 0.16 seconds
这会得到您想要的结果,但您可能需要考虑执行时间。
希望对您有所帮助