您需要 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)})