数据湖探索 DLI-rank:示例代码

时间:2024-09-27 11:19:00

示例代码

为便于理解函数的使用方法,本文为您提供源数据,基于源数据提供函数相关示例。创建表logs,并添加数据,命令示例如下:
CREATE TABLE logs (
cookieid string,
createtime string,
pv INT
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' 
stored as textfile;

添加数据如下:

cookie1 2015-04-10 1
cookie1 2015-04-11 5
cookie1 2015-04-12 7
cookie1 2015-04-13 3
cookie1 2015-04-14 2
cookie1 2015-04-15 4
cookie1 2015-04-16 4
cookie2 2015-04-10 2
cookie2 2015-04-11 3
cookie2 2015-04-12 5
cookie2 2015-04-13 6
cookie2 2015-04-14 3
cookie2 2015-04-15 9
cookie2 2015-04-16 7

示例:将所有记录根据cookieid分组,并按pv降序排列,返回组内每行的序号。命令示例如下:

select cookieid, createtime, pv,
       rank() over(partition by cookieid order by pv desc) as rank
from logs
where cookieid = 'cookie1';
-- 结果:
cookie1 2015-04-12 7 1
cookie1 2015-04-11 5 2
cookie1 2015-04-16 4 3 (并列第三)
cookie1 2015-04-15 4 3
cookie1 2015-04-13 3 5 (跳过4,从5开始)
cookie1 2015-04-14 2 6
cookie1 2015-04-10 1 7
support.huaweicloud.com/sqlref-spark-dli/dli_spark_rank.html