如何使用 Hive/Pig/MapReduce 展平递归层次结构 [英] How to flatten recursive hierarchy using Hive/Pig/MapReduce
本文介绍了如何使用 Hive/Pig/MapReduce 展平递归层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以表格格式存储了不平衡的树数据,例如:
I have unbalanced tree data stored in tabular format like:
parent,child
a,b
b,c
c,d
c,f
f,g
树的深度未知.
如何将这个层次结构展平,其中每一行都包含从叶节点到根节点的完整路径,如下所示:
how to flatten this hierarchy where each row contains entire path from leaf node to root node in a row as:
leaf node, root node, intermediate nodes
d,a,d:c:b
f,a,e:b
是否有使用 hive、pig 或 mapreduce 解决上述问题的建议?提前致谢.
Any suggestions to solve above problem using hive, pig or mapreduce? Thanks in advance.
推荐答案
我尝试用pig解决,示例代码如下:
I tried to solve it using pig, here are the sample code:
加入功能:
-- Join parent and child
Define join_hierarchy ( leftA, source, result) returns output {
joined= join $leftA by parent left, $source by child;
tmp_filtered= filter joined by source::parent is null;
part= foreach tmp_filtered leftA::child as child, leftA::path as path;
$result= union part, $result;
part_remaining= filter joined by source::parent is not null;
$output= foreach part_remaining generate $leftA::child as child, source::parent as parent, concat(concat(source::parent,':'),$leftA::path)
}
加载数据集:
--My dataset field delimiter is ','.
source= load '*****' using pigStorage(',') as (parent:chararray, child:chararray);
--create additional column for path
leftA= foreach source generate child, parent, concat(parent,':');
--initially result table will be blank.
result= limit leftA 1;
result= foreach result generate '' as child , '' as parent;
--Flatten hierarchy to 4 levels. Add below lines equivalent to hierarchy depth.
leftA= join_hierarchy(leftA, source, result);
leftA= join_hierarchy(leftA, source, result);
leftA= join_hierarchy(leftA, source, result);
leftA= join_hierarchy(leftA, source, result);
这篇关于如何使用 Hive/Pig/MapReduce 展平递归层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文