I can\'t understand HiveQL rank(). I\'ve found a couple of implementations of rank UDF\'s on the WWW, such as Edward\'s nice example. I can load and access the functions, bu
If you have a evaluate function as below, assuming you are using the function directly form the guide you mentioned,
private long counter;
@Override
public Object evaluate(DeferredObject[] currentKey) throws HiveException {
if (!sameAsPreviousKey(currentKey)) {
this.counter = 0;
copyToPreviousKey(currentKey);
}
return new Long(++this.counter);
}
try changing it to the following, so that the counter is not reset when it finds a new volume, rather you don't increment if you find the same volume but increment only when it finds a new volume.
private long counter;
@Override
public Object evaluate(DeferredObject[] currentKey) throws HiveException {
//when not same as previous key you rather increment
if (!sameAsPreviousKey(currentKey)) {
this.counter ++;
copyToPreviousKey(currentKey);
}
//else you keep the counter as it is
return new Long(++this.counter);
}
Tell me if this helps.
With the Windowing and Analytics functions introduced in Hive 0.11, you can use:
select SalesRepId, volume as amount , rank() over (order by V.volume desc) as rank from
(select SalesRepId,sum(amount) as volume from purchases group by SalesRepId) V;