3

hive利用rank函数进行排名

 3 years ago
source link: http://www.hechunbo.com/index.php/archives/375.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

创建测试数据

drop table score;
OK
Time taken: 0.265 seconds
hive>

> create table score(name string,subject string,score int)
> row format delimited
> fields terminated by '\t';

OK
Time taken: 0.166 seconds
hive> load data local inpath '/opt/score.txt' into table score;
Loading data to table default.score

score.txt的内容可以从下面复制。

查询score

select * from score;
OK
悟空    语文    87
悟空    数学    95
悟空    英语    68
八戒    语文    94
八戒    数学    56
八戒    英语    84
张飞    语文    64
张飞    数学    86
张飞    英语    84
小乔    语文    65
小乔    数学    85
小乔    英语    78

不分组只对分数进行排名。 如果分组的话加上partition by subject

select *,rank() over( order by score desc) rank from score;

Query ID = hcb_20210413073842_e343c9f8-8d7c-4c9a-b3c1-e40905772829
Total jobs = 1
Launching Job 1 out of 1
Status: Running (Executing on YARN cluster with App id application_1618226757678_0001)

VERTICES      MODE        STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED

Map 1 .......... container SUCCEEDED 1 1 0 0 0 0

Reducer 2 ...... container SUCCEEDED 1 1 0 0 0 0

VERTICES: 02/02 [==========================>>] 100% ELAPSED TIME: 14.22 s

OK
悟空 数学 95 1
八戒 语文 94 2
悟空 语文 87 3
张飞 数学 86 4
小乔 数学 85 5
八戒 英语 84 6
张飞 英语 84 6
小乔 英语 78 8
悟空 英语 68 9
小乔 语文 65 10
张飞 语文 64 11
八戒 数学 56 12


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK