Flatten tuple like a bag

百般思念 提交于 2019-12-05 07:53:59

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);

The magic part is the TOBAG operator.

You can use DataFu's UDF TransposeTupleToBag (http://datafu.incubator.apache.org/docs/datafu/1.1.0/datafu/pig/util/TransposeTupleToBag.html) and then flatten the bag to get a row per item in the bag.

I know this is an old thread but I could not get the above method to work. Thought I will share my findings.

input: (1-2-3, abc)
       (4-5-6, xyz)
desired output:
       (1, abc)
       (2, abc)
       (3, abc)
       (4, xyz)
       (5, xyz)
       (6, xyz)

Initially, I used STRSPLIT that generates a tuple resulting in the similar input as above but was unsuccessful.

output = FOREACH input GENERATE FLATTEN(TOBAG(STRSPLIT($0, '-'))), $1

This resulted in the output as:

 (1,2,3,abc)
 (4,5,6,xyz)

However, when I used tokenize and replace functions I got the desired output.

output = FOREACH input GENERATE FLATTEN(TOKENIZE(REPLACE($0,'-', ' '))), $1;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!