1

所以,我有一个数据,它有两个值,字符串和一个数字。

data(string:chararray, number:int)

我计算了 5 条不同的规则,

1:int为0~1。

2:int为1~2。

~

5:int为4~5。

所以我能够单独计算它们,

zero_to_one = filter avg_user by average_stars >= 0 and average_stars <= 1;
A = GROUP zero_to_one ALL;
zto_count = FOREACH A GENERATE COUNT(zero_to_one);

one_to_two = filter avg_user by average_stars > 1 and average_stars <= 2;
B = GROUP one_to_two ALL;
ott_count = FOREACH B GENERATE COUNT(one_to_two);

two_to_three = filter avg_user by average_stars > 2 and average_stars <= 3;
C = GROUP two_to_three ALL;
ttt_count = FOREACH C GENERATE COUNT( two_to_three);

three_to_four = filter avg_user by average_stars > 3 and average_stars <= 4;
D = GROUP three_to_four ALL;
ttf_count = FOREACH D GENERATE COUNT( three_to_four);

four_to_five = filter avg_user by average_stars > 4 and average_stars <= 5;
E = GROUP four_to_five ALL;
ftf_count = FOREACH E GENERATE COUNT( four_to_five);

因此,可以这样做,但这只会产生 5 个单独的表。

我想看看有什么办法(可以花哨,我喜欢花哨的东西) T 可以在单表中生成结果。

这意味着如果

zto_count = 1
ott_count = 3
. = 2
. = 3
. = 5

那么表格将是 {1,3,2,3,5}

解析数据并以这种方式组织它们很容易。

有什么办法吗?

4

1 回答 1

2

使用它作为输入:

foo 2
foo 3
foo 2
foo 3
foo 5
foo 4
foo 0
foo 4
foo 4
foo 5
foo 1
foo 5

(0和1各出现一次,2和3各出现两次,4和5各出现三次)

这个脚本:

A = LOAD 'myData' USING PigStorage(' ') AS (name: chararray, number: int);

B = FOREACH (GROUP A BY number) GENERATE group AS number, COUNT(A) AS count ;

C = FOREACH (GROUP B ALL) {
    zto = FOREACH B GENERATE (number==0?count:0) + (number==1?count:0) ;
    ott = FOREACH B GENERATE (number==1?count:0) + (number==2?count:0) ;
    ttt = FOREACH B GENERATE (number==2?count:0) + (number==3?count:0) ;
    ttf = FOREACH B GENERATE (number==3?count:0) + (number==4?count:0) ;
    ftf = FOREACH B GENERATE (number==4?count:0) + (number==5?count:0) ;
    GENERATE SUM(zto) AS zto,
             SUM(ott) AS ott,
             SUM(ttt) AS ttt,
             SUM(ttf) AS ttf,
             SUM(ftf) AS ftf ;
}

产生这个输出:

C: {zto: long,ott: long,ttt: long,ttf: long,ftf: long}
(2,3,4,5,6)

C 中 FOREACH 的数量并不重要,因为 C 最多只有 5 个元素,但如果是这样,那么它们可以像这样放在一起:

C = FOREACH (GROUP B ALL) {
    total = FOREACH B GENERATE (number==0?count:0) + (number==1?count:0) AS zto,
                               (number==1?count:0) + (number==2?count:0) AS ott,
                               (number==2?count:0) + (number==3?count:0) AS ttt,
                               (number==3?count:0) + (number==4?count:0) AS ttf,
                               (number==4?count:0) + (number==5?count:0) AS ftf ;
    GENERATE SUM(total.zto) AS zto,
             SUM(total.ott) AS ott,
             SUM(total.ttt) AS ttt,
             SUM(total.ttf) AS ttf,
             SUM(total.ftf) AS ftf ;
}
于 2013-07-02T21:08:03.913 回答