HiveQL and rank()

前端 未结 2 866
终归单人心
终归单人心 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条回答
  •  萌比男神i
    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.

提交回复
热议问题