【发布时间】:2014-04-16 09:41:29
【问题描述】:
我想对列的子集执行 DISTINCT 操作。
A = LOAD 'data' AS(a1,a2,a3,a4,a5,a6);
DUMP A;
(1, 2, 3, 4,5,5_1)
(1, 2, 3, 4,5,5_1)
(1, 2, 3, 4,6,6_1)
(1 ,2, 4, 4,7,7_1)
(1, 2, 4, 4,8,8_1)
-- insert DISTINCT operation on a1,a2,a3,a4 here:
-- ...
DUMP A_unique;
(1, 2, 3, 4,5,5_1)
(1, 2, 4, 4,7,7_1)
我已经参考了链接:
How to perform a DISTINCT in Pig Latin on a subset of columns?
并使用了以下两种方式:
方法一:
1.DATA = LOAD '/usr/local/Input.txt' AS (a1,a2,a3,a4,a5,a6);
2.DATA2 = FOREACH DATA GENERATE TOTUPLE(a1,a2,a3,a4) AS combined, a5 as a5,a6 as a6;
3.grouped_by_a5_a6 = GROUP DATA2 BY combined;
4.grouped_and_distinct = FOREACH grouped_by_a5_a6 {
combined_unique =LIMIT DATA2 1;
GENERATE FLATTEN(combined_unique);
};
方法二:
DATA = LOAD '/usr/local/Input.txt' AS (a1,a2,a3,a4,a5,a6) ;
A2 = FOREACH DATA GENERATE TOTUPLE(a1,a2,a3,a4) AS combined, a5 as a5,a6 as a6 ;
grouped_by_a5_a6 = GROUP A2 BY (a5,a6);
grouped_and_distinct = FOREACH grouped_by_a5_a6 {
combined_unique = DISTINCT A2.combined;
GENERATE FLATTEN(combined_unique);
};
但我得到的答案是:
(1, 2, 3, 4,5,5_1)
(1, 2, 3, 4,6,6_1)
(1, 2, 4, 4,7,7_1)
(1, 2, 4, 4,8,8_1)
代替:
(1, 2, 3, 4,5,5_1)
(1, 2, 4, 4,7,7_1)
上述代码有什么问题?
【问题讨论】:
-
为什么你认为 (1, 2, 3, 4,6,6_1) 和 (1, 2, 4, 4,8,8_1) 不应该出现在输出中?我看不出有什么理由会过滤掉它们,而且对我来说这看起来是正确的结果。
-
bridiver 你是对的,但我需要我提到的输出。您能建议更改代码吗?
-
我确定您为什么希望过滤掉其他记录。过滤的依据是什么?
标签: apache-pig