Apache Pig - nested FOREACH over same relation

那年仲夏 提交于 2019-12-25 08:21:17

问题


I have a number of bags and I want to compute the pairwise similarities between the bags.

sequences = FOREACH raw GENERATE gen_bag(logs);

The relation is described as follows:

sequences: {t: (type: chararray, value:charray)}

The similarity is computed by a Python UDF that takes two bags as arguments. I have tried to do a nested foreach over the sequences variable, but I cant loop over the same relation twice. I've also tried to define the sequences twice, but I cant access the copy in the foreach. I'm also unsure how I can come up with a data structure that allows me to do things like this. How can I do this?


回答1:


It sounds like you could load up two copies of the source data, perform a cross-product, then iterate through calling your UDF on each pair. Something like:

sequences_A = FOREACH raw GENERATE gen_bag(logs);
sequences_B = FOREACH raw GENERATE gen_bag(logs);
all_pairs = CROSS sequences_A, sequences_B;
FOREACH all_pairs GENERATE myudf(first_bag, second_bag);


来源:https://stackoverflow.com/questions/40358849/apache-pig-nested-foreach-over-same-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!