像袋子一样压扁元组 [英] Flatten tuple like a bag

查看:39
本文介绍了像袋子一样压扁元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集如下所示:

( A, (1,2) )
( B, (2,9) )

我想展平"Pig 中的元组,基本上为内部元组中找到的每个值重复每条记录,这样预期的输出是:

I would like to "flatten" the tuples in Pig, basically repeating each record for each value found in the inner-tuple, such that the expected output is:

( A, 1 )
( A, 2 )
( B, 2 ) 
( B, 9 )

我知道当元组 (1,2) 和 (2,9) 是袋子时,这是可能的.

I know this is possible when the tuples (1,2) and (2,9) are bags instead.

推荐答案

你的洞察力很好;可以通过在包中转换元组来实现.我们要针对的模式是:{a: chararray,{(chararray)}} 例如:(A,{(1),(2)})

Your insight is good; it's possible by transforming the tuple in a bag. The schema we want to aim for is: {a: chararray,{(chararray)}} for example: (A,{(1),(2)})

这是您问题的解决方案:

Here is the solution to your problem:

A = LOAD 'data.txt' AS (a:chararray,b:(b1:chararray,b2:chararray));
B = FOREACH A GENERATE a, TOBAG(b.b1,b.b2);
C = FOREACH B GENERATE a, FLATTEN($1);

神奇的部分是 TOBAG 运算符.

The magic part is the TOBAG operator.

这篇关于像袋子一样压扁元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆