Usage of Apache Pig rank function

核能气质少年 提交于 2019-12-19 06:58:55

问题


Am using Pig 0.11.0 rank function and generating ranks for every id in my data. I need ranking of my data in a particular way. I want the rank to reset and start from 1 for every new ID.

Is it possible to use the rank function directly for the same? Any tips would be appreciated.

Data:

id,rating
X001, 9
X001, 9
X001, 8
X002, 9
X002, 7
X002, 6
X002, 5
X003, 8
X004, 8
X004, 7
X004, 7
X004, 4

On using rank function like: op = rank data by id,score;

I get this output

rank,id,rating
1, X001, 9
1, X001, 9
2, X001, 8
3, X002, 9
4, X002, 7
5, X002, 6
6, X002, 5
7, X003, 8
8, X004, 8
9, X004, 7
9, X004, 7
10, X004, 4

Desired O/P:

rank,id,rating
1, X001, 9
1, X001, 9
2, X001, 8
1, X002, 9
2, X002, 7
3, X002, 6
4, X002, 5
1, X003, 8
1, X004, 8
2, X004, 7
2, X004, 7
3, X004, 4

回答1:


You can group your data by id then use the UDF Enumerate (DataFu) to append an index to each tuple of the bags.

register datafu-1.1.0.jar;
define Enumerate datafu.pig.bags.Enumerate('1');

data = load 'data' using PigStorage(',') as (id:chararray, rating:int);
data = group data by id;
data = foreach data {
  sorted = order data by rating DESC;
  generate group, sorted;
}
data = foreach data generate FLATTEN(Enumerate(sorted));
data = foreach data generate $2, $0, $1;
dump data;

DataFu jar file can be downloaded from Maven Central repository: http://search.maven.org/#search|ga|1|g%3A%22com.linkedin.datafu%22




回答2:


You can use RANK function as below: B = rank A by rating DESC; dump B;

Note: considering A having (id, rating) mentioned in your example.



来源:https://stackoverflow.com/questions/22987036/usage-of-apache-pig-rank-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!