Pigscript逻辑实现

时间:2017-06-06 15:25:44

标签: apache-pig

我是pigcript的新手。我在下面的例子中遇到困难。任何人都可以帮助我如何使用pigscript获得下面指定的输出?

输入:

1|ABC|NC  
1|DEF|NC  
2|CFD|NY  
2|CGF|NY

输出:

1|ABC,DEF|NC  
2|CFD,CGF|NY

脚本:

A = LOAD 'testfile.txt' USING PigStorage('|') AS (Id:chararray,name:chararray,state:chararray);
B = FOREACH A GENERATE Id,name;
C = FOREACH A GENERATE Id,name,state;
C = DISTINCT C;
GROUPED = GROUP B BY Id;
D = FOREACH GROUPED GENERATE group AS Id,c.name AS name_val;
E = JOIN D BY Id, C BY Id;
X = FOREACH E GENERATE D.Id as docid,D.name_val as termid,C.state;
Dump X;

2 个答案:

答案 0 :(得分:0)

按第1列和第3列加载数据和组,然后生成列,以获得所需的输出。

A = LOAD 'input.txt' USING PigStorage('|') AS (f1:int,f2:chararray;f3:chararray);
B = GROUP A BY f1,f3;
C = FOREACH B GENERATE FLATTEN(group) as (f1,f3),A.f2 AS f2;
D = FOREACH C GENERATE f1,f2,f3;
DUMP D;

答案 1 :(得分:0)

Its working as now expected after adding BagToString method.

A = LOAD 'testfile.txt' USING PigStorage('|') AS (f1:int,f2:chararray,f3:chararray);          
B = GROUP A BY (f1,f3);        
C = FOREACH B GENERATE FLATTEN(group) as (f1,f3),A.f2 AS f2;  
D = FOREACH C GENERATE f1,  BagToString(f2, ','), f3;  
STORE D INTO '[path]' USING PigStorage('|');