0

我有一个名为 stuff 的关系,其架构如下:

grunt> describe stuff;
stuff: {child_id: long,parent_id: long} 

我想通过 parent_id 对 child_ids 进行分组,然后生成一个输出作为不同 child_ids 之间的有序对列表。例如,如果 parent_id 为 100 下有 child_id 1,2,4 和 5,我想要的输出是这样的:

1,2
1,4
1,5
2,1
2,4
2,5
4,1
4,2
4,5
5,1
5,2
5,4

我必须编写一个 eval 函数吗?

4

1 回答 1

1

您需要 CROSS 运算符。这是一个例子:

输入

1,2
1,1
1,3
1,4
2,5
2,3
2,6

代码

inpt = load 'parent_child.csv' using PigStorage(',') as (parent_id: long, child_id: long);
tmp = foreach inpt generate parent_id, child_id as b1, child_id as b2; -- needed to use CROSS in the nested FOREACH
parentGroup = group tmp by parent_id;

perms = foreach parentGroup { 
    bro_1 = tmp.b1;
    bro_2 = tmp.b2;
    brothers = cross bro_1, bro_2;
    brothers = filter brothers by b1 != b2; -- remove relationship to itself
    generate group as parent_id, brothers;
}

输出

架构-烫发:{parent_id:long,兄弟:{(bro_1 :: b2:long,bro_2 :: b2:long)}}

(1,{(1,2),(3,2),(4,2),(2,1),(3,1),(4,1),(2,3),(1,3),(4,3),(2,4),(1,4),(3,4)})
(2,{(3,5),(6,5),(5,3),(6,3),(5,6),(3,6)})
于 2013-01-09T10:14:15.337 回答