在 Pig Script 中根据总和查找 5 个最流行的
Find 5 top popular based on sum in Pig Script
我正在尝试查找 tripCount 最多的前 3 个最受欢迎的地点。
所以我需要查看每个位置的总数 tripCount
和 return 最大的 n...
我的数据如下:
LocationID tripCount tripDistance
101 40 4.6
203 29 1.3
56 25 9.3
101 17 4.5
66 5 1.1
13 5 0.5
203 10 1.2
558 8 0.5
56 10 5.5
所以我期待的结果是:
101 57
203 39
56 35
到目前为止我的代码是:
B = GROUP UNION_DATA BY DOLocationID;
C = FOREACH B {
DA = ORDER UNION_DATA BY passenger_count DESC;
DB = LIMIT DA 5;
GENERATE FLATTEN(group), FLATTEN(DB.LocationID), FLATTEN(DB.dropoff_datetime);
}
我错过了什么,我需要做什么才能获得预期的结果?
下面的一段代码应该可以得到您想要的结果。
为了更好地理解,我将该语句分解为简单的块,并且 readability.Also 您提供的别名和代码似乎不完整,因此我完全从头开始重写。
LocationID、tripCount、tripDistance
cat > trip_data.txt
101,40,4.6
203,29,1.3
56,25,9.3
101,17,4.5
66,5,1.1
13,5,0.5
203,10,1.2
558,8,0.5
56,10,5.5
猪码:
A = load '/home/ec2-user/trip_data.txt' using PigStorage(',') as (LocationID,tripCount,tripDistance);
describe A;
B = GROUP A BY LocationID;
describe B;
dump B;
C = FOREACH B GENERATE group, SUM(A.tripCount);
describe C;
dump C;
D = ORDER C BY DESC;
describe D;
dump D;
RESULT = LIMIT D 3;
describe RESULT;
dump RESULT;
我正在尝试查找 tripCount 最多的前 3 个最受欢迎的地点。
所以我需要查看每个位置的总数 tripCount
和 return 最大的 n...
我的数据如下:
LocationID tripCount tripDistance
101 40 4.6
203 29 1.3
56 25 9.3
101 17 4.5
66 5 1.1
13 5 0.5
203 10 1.2
558 8 0.5
56 10 5.5
所以我期待的结果是:
101 57
203 39
56 35
到目前为止我的代码是:
B = GROUP UNION_DATA BY DOLocationID;
C = FOREACH B {
DA = ORDER UNION_DATA BY passenger_count DESC;
DB = LIMIT DA 5;
GENERATE FLATTEN(group), FLATTEN(DB.LocationID), FLATTEN(DB.dropoff_datetime);
}
我错过了什么,我需要做什么才能获得预期的结果?
下面的一段代码应该可以得到您想要的结果。 为了更好地理解,我将该语句分解为简单的块,并且 readability.Also 您提供的别名和代码似乎不完整,因此我完全从头开始重写。
LocationID、tripCount、tripDistance
cat > trip_data.txt
101,40,4.6
203,29,1.3
56,25,9.3
101,17,4.5
66,5,1.1
13,5,0.5
203,10,1.2
558,8,0.5
56,10,5.5
猪码:
A = load '/home/ec2-user/trip_data.txt' using PigStorage(',') as (LocationID,tripCount,tripDistance);
describe A;
B = GROUP A BY LocationID;
describe B;
dump B;
C = FOREACH B GENERATE group, SUM(A.tripCount);
describe C;
dump C;
D = ORDER C BY DESC;
describe D;
dump D;
RESULT = LIMIT D 3;
describe RESULT;
dump RESULT;