非常感谢任何帮助!最好的方法是举个例子:
输入:
架构:
名称| PHONE_TYPE | PHONE_NUM
示例数据:
凯尔|单元格| 555-222-3333
凯尔|主页| 453-444-5555
汤|首页| 555-555-5555
汤姆|寻呼机| 555-555-4344
期望的输出:
架构:
名称| Home_num | Cell_num | Pager_num
示例:
凯尔| 453-444-5555 | 555-222-3333 |空
汤姆| 555-555-5555 |空| 555-555-4344
代码:
data=Load 'test.txt' using PigStorage('|');
grpd= Group data by $0;
Foreach grpd{
???
}
答案 0 :(得分:2)
在@Murali老挝的评论之后,我改写了解决方案。
我现在使用FILTER,然后不用FLATTEN过滤空袋的技巧是在袋子为空时添加空字符串。
以下是我的测试数据:
tom,home,555
tom,pager,666
tom,cell,777
bob,home,111
bob,cell,222
这是我的解决方案:
data = LOAD 'phone' USING PigStorage(',') AS (name:chararray, phone_type: chararray, phone_num: chararray);
user = FOREACH (GROUP data BY name) {
home = FILTER $1 BY phone_type == 'home';
-- you add an empty string if the the bag is empty
homenum = (IsEmpty(home) ? {('')} : home.phone_num);
pager = FILTER $1 BY phone_type == 'pager';
pagernum = (IsEmpty(pager) ? {('')} : pager.phone_num);
cell = FILTER $1 BY phone_type == 'cell';
cellnum = (IsEmpty(cell) ? {('')} : cell.phone_num);
GENERATE group as name, FLATTEN(homenum) as home, FLATTEN(pagernum) as pager, FLATTEN(cellnum) as cell;
};
转储后,我获得以下结果:
(bob,111,,222)
(tom,555,666,777)