hiveql和rank()

yhuiod9q  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(385)

我无法理解hiveql rank()。我在www上找到了rank-udf的几个实现,比如edward的好例子。我可以加载和访问函数,但我不能让它们做我想做的事情。下面是一个详细的示例:
将udf加载到cli进程:

$ javac -classpath /home/hadoop/hadoop/hadoop-core-1.0.4.jar:/home/hadoop/hive/lib/hive-exec-0.10.0.jar com/m6d/hiveudf/Rank2.java 
$ jar -cvf Rank2.jar com/m6d/hiveudf/Rank2.class
hive> ADD JAR /home/hadoop/MyDemo/Rank2.jar;
hive> CREATE TEMPORARY FUNCTION Rank2 AS 'com.m6d.hiveudf.Rank2';

创建表:

create table purchases (
  SalesRepId String, 
  PurchaseOrderId INT, 
  Amount INT
) 
ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ','
  LINES TERMINATED BY '\n';

从此csv加载数据:

Jana,1,100
Nadia,2,200
Nadia,3,600
Daniel,4,80
Jana,5,120
William,6,170
Daniel,7,140

通过cli:

LOAD DATA 
  LOCAL INPATH '/home/hadoop/MyDemo/purchases.csv'
  INTO TABLE purchases;

现在我可以看到我的顶级销售代表:

select SalesRepId,sum(amount) as volume
from purchases
group by SalesRepId
ORDER BY volume DESC;

娜迪亚卖了800美元,丹尼尔和雅娜都卖了220美元,威廉卖了170美元

SalesRep    Amount
--------    ------
Nadia       800
Daniel      220
Jana        220
William     170

现在我只想给他们编号:娜迪亚是1号,丹尼尔和雅娜并列2号,威廉是4号(不是3号)

select SalesRepId, V.volume,rank2(V.volume)
from 
(select SalesRepId,sum(amount) as volume
from purchases
group by SalesRepId
ORDER BY volume DESC) V;

这是我得到的,但不是我想要的:

SalesRep   Amount  Rank
--------   ------  ----
Nadia       800      1
Daniel      220      1
Jana        220      2
William     170      1

这是我想要的,但我不能让hive帮我做:

SalesRep   Amount  Rank
--------   ------  ----
Nadia       800      1
Daniel      220      2
Jana        220      2
William     170      4

你能帮我用正确的hiveql给我的销售代表排名吗?
感谢jtherocker的回应。他的变化导致了这个清单:

SalesRep   Amount  Rank
--------   ------  ----
William     170     1
Daniel      220     2
Jana        220     2
Nadia       800     3

稍微修改一下,显示纳迪亚是第四名(不是第三名):

private row_number;
@Override
public Object evaluate(DeferredObject[] currentKey) throws HiveException {
  row_number++;
  if (!sameAsPreviousKey(currentKey)) {
    this.counter = row_number;
    copyToPreviousKey(currentKey);
  }
return new Long(this.counter);
}
pvcm50d1

pvcm50d11#

通过hive 0.11中引入的窗口和分析功能,您可以使用:

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;
ve7v8dk2

ve7v8dk22#

如果您有下面的求值函数,假设您直接从您提到的指南中使用该函数,

private long counter;
@Override
  public Object evaluate(DeferredObject[] currentKey) throws HiveException {
    if (!sameAsPreviousKey(currentKey)) {
      this.counter = 0;
      copyToPreviousKey(currentKey);
    }

    return new Long(++this.counter);
  }

尝试将其更改为以下值,以便计数器在找到新卷时不重置,而在找到相同卷时不递增,而仅在找到新卷时递增。

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);
 }

告诉我这是否有用。

相关问题