HiveQL and rank()

前端 未结 2 846
终归单人心
终归单人心 2021-01-07 04:22

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

相关标签:
2条回答
  • 2021-01-07 04:49

    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.

    0 讨论(0)
  • 2021-01-07 04:54

    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;
    
    0 讨论(0)
提交回复
热议问题