我有一个示例,我们试图做一个看似简单的联接:
A = load 'data6' as ( item:chararray, d:int, things:bag{(thing:chararray, d1:int, values:bag{(v:chararray)})} );
B = load 'data7' as ( v:chararray, r:chararray );
grunt> cat data1
'item1' 111 { ('thing1', 222, {('value1'),('value2')}) }
grunt> cat data2
'value1' 'result1'
'value2' 'result2'
我们想将
'result1'
的'result2'
,data2
数据加入到data1
字段中明显的value
的条目中。我们设法将其展平:
A = load 'data6' as ( item:chararray, d:int, things:bag{(thing:chararray, d1:int, values:bag{(v:chararray)})} );
B = load 'data7' as ( v:chararray, r:chararray );
F1 = foreach A generate item, d, flatten(things);
F2 = foreach F1 generate item..d1, flatten(values);
然后,我们将第二个数据集加入:
J = join F2 by v, B by v
J1 = foreach J generate item as item, d as d, thing as thing, d1 as d1, F2::things::values::v as v, r as r; --Remove duplicate field & clean up naming
dump J1
('item1',111,'thing1',222,'value1','result1')
('item1',111,'thing1',222,'value2','result2')
现在,我们需要为每个项目调用一次UDF函数,因此我们需要将这两个级别的袋子重新分组。每个项目都有0个或更多的事物,并且每个事物都有0个或多个值,并且现在这些值可能有也可能没有结果。
我们如何回到:
('item1', 111, { 'thing1', 222, { ('value1, 'result1'), ('value2', 'result2') }
我对分组和重新加入的所有尝试都变得十分复杂,未能产生正确的结果,并且在4个以上的mapreduce作业中运行,而在Hadoop中应该是1个mapreduce作业。
最佳答案
以下代码可能有效,R2是最终结果:
group_by_item_d_thing_d1 = group J1 by item, d, thing, d1;
R1 = foreach group_by_item_d_thing_d1 generate group.item, group.d, group.thing, group.d1, J1;
group_by_item_d = group R1 by item, d;
R2 = foreach group_by_item_d generate group.item, group.d, R1;
关于hadoop - pig :如何弄平并重新连接袋子中的袋子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16951814/