Spark sql -- Spark sql中的窗口函数和对应的api
一、窗口函数种类
- ranking 排名类
- analytic 分析类
- aggregate 聚合类
| Function Type | SQL | DataFrame API | Description |
| Ranking | rank | rank | rank值可能是不连续的 |
| Ranking | dense_rank | denseRank | rank值一定是连续的 |
| Ranking | percent_rank | percentRank | 相同的分组中 (rank -1) / ( count(score) - 1 ) |
| Ranking | ntile | ntile | 将同一组数据循环的往n个桶中放,返回对应的桶的index,index从1开始 |
| Ranking | row_number | rowNumber | 很单纯的行号,类似excel的行号 |
| Analytic | cume_dist | cumeDist | |
| Analytic | first_value | firstValue | 相同的分组中最小值 |
| Analytic | last_value | lastValue | 相同的分组中最大值 |
| Analytic | lag | lag | 取前n行数据 |
| Analytic | lead | lead | 取后n行数据 |
| Aggregate | min | min | 最小值 |
| Aggregate | max | max | 最大值 |
| Aggregate | sum | sum | 求和 |
| Aggregate | avg | avg | 求平均 |
二、具体用法如下
count(...) over(partition by ... order by ...)--求分组后的总数。
sum(...) over(partition by ... order by ...)--求分组后的和。
max(...) over(partition by ... order by ...)--求分组后的最大值。
min(...) over(partition by ... order by ...)--求分组后的最小值。
avg(...) over(partition by ... order by ...)--求分组后的平均值。
rank() over(partition by ... order by ...)--rank值可能是不连续的。
dense_rank() over(partition by ... order by ...)--rank值是连续的。
first_value(...) over(partition by ... order by ...)--求分组内的第一个值。
last_value(...) over(partition by ... order by ...)--求分组内的最后一个值。
lag() over(partition by ... order by ...)--取出前n行数据。
lead() over(partition by ... order by ...)--取出后n行数据。
ratio_to_report() over(partition by ... order by ...)--Ratio_to_report() 括号中就是分子,over() 括号中就是分母。
percent_rank() over(partition by ... order by ...)--
三、实际例子
案例数据:/root/score.json/score.json,学生名字、课程、分数
{"name":"A","lesson":"Math","score":100}
{"name":"B","lesson":"Math","score":100}
{"name":"C","lesson":"Math","score":99}
{"name":"D","lesson":"Math","score":98}
{"name":"A","lesson":"E","score":100}
{"name":"B","lesson":"E","score":99}
{"name":"C","lesson":"E","score":99}
{"name":"D","lesson":"E","score":98}
select
name,lesson,score,
ntile(2) over (partition by lesson order by score desc ) as ntile_2,
ntile(3) over (partition by lesson order by score desc ) as ntile_3,
row_number() over (partition by lesson order by score desc ) as row_number,
rank() over (partition by lesson order by score desc ) as rank,
dense_rank() over (partition by lesson order by score desc ) as dense_rank,
percent_rank() over (partition by lesson order by score desc ) as percent_rank
from score
order by lesson,name,score
输出结果完全一样,如下表所示
| name | lesson | score | ntile_2 | ntile_3 | row_number | rank | dense_rank | percent_rank |
|---|---|---|---|---|---|---|---|---|
| A | E | 100 | 1 | 1 | 1 | 1 | 1 | 0.0 |
| B | E | 99 | 1 | 1 | 2 | 2 | 2 | 0.3333333333333333 |
| C | E | 99 | 2 | 2 | 3 | 2 | 2 | 0.3333333333333333 |
| D | E | 98 | 2 | 3 | 4 | 4 | 3 | 1.0 |
| A | Math | 100 | 1 | 1 | 1 | 1 | 1 | 0.0 |
| B | Math | 100 | 1 | 1 | 2 | 1 | 1 | 0.0 |
| C | Math | 99 | 2 | 2 | 3 | 3 | 2 | 0.6666666666666666 |
| D | Math | 98 | 2 | 3 | 4 | 4 | 3 | 1.0 |
参考:
=================================================================================
原创文章,转载请务必将下面这段话置于文章开头处(保留超链接)。
本文转发自程序媛说事儿,原文链接https://www.cnblogs.com/abc8023/p/10910741.html
=================================================================================
Spark sql -- Spark sql中的窗口函数和对应的api的更多相关文章
- Apache Spark 2.2.0 中文文档 - Spark SQL, DataFrames and Datasets Guide | ApacheCN
Spark SQL, DataFrames and Datasets Guide Overview SQL Datasets and DataFrames 开始入门 起始点: SparkSession ...
- Apache Spark 2.2.0 中文文档 - Spark SQL, DataFrames and Datasets
Spark SQL, DataFrames and Datasets Guide Overview SQL Datasets and DataFrames 开始入门 起始点: SparkSession ...
- Spark2.x学习笔记:Spark SQL的SQL
Spark SQL所支持的SQL语法 select [distinct] [column names]|[wildcard] from tableName [join clause tableName ...
- 大数据技术之_27_电商平台数据分析项目_02_预备知识 + Scala + Spark Core + Spark SQL + Spark Streaming + Java 对象池
第0章 预备知识0.1 Scala0.1.1 Scala 操作符0.1.2 拉链操作0.2 Spark Core0.2.1 Spark RDD 持久化0.2.2 Spark 共享变量0.3 Spark ...
- [Spark] 05 - Spark SQL
关于Spark SQL,首先会想到一个问题:Apache Hive vs Apache Spark SQL – 13 Amazing Differences Hive has been known t ...
- Hive on Spark和Spark sql on Hive,你能分的清楚么
摘要:结构上Hive On Spark和SparkSQL都是一个翻译层,把一个SQL翻译成分布式可执行的Spark程序. 本文分享自华为云社区<Hive on Spark和Spark sql o ...
- SQL Server中的窗口函数
简介 SQL Server 2012之后对窗口函数进行了极大的加强,但对于很多开发人员来说,对窗口函数却不甚了解,导致了这样强大的功能被浪费,因此本篇文章主要谈一谈SQL Server中窗口函 ...
- [Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子
[Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子 $cat people.json {"name":" ...
- [Spark][Hive][Python][SQL]Spark 读取Hive表的小例子
[Spark][Hive][Python][SQL]Spark 读取Hive表的小例子$ cat customers.txt 1 Ali us 2 Bsb ca 3 Carls mx $ hive h ...
随机推荐
- docker学习9-搭建rabbitMQ环境
前言 docker搭建rabbitMQ环境 下载镜像 rabbitMQ 镜像仓库地址https://hub.docker.com/_/rabbitmq 找带有 mangement的版本,会带后台管理界 ...
- Docker 中 MySQL 数据的导入导出
Creating database dumps Most of the normal tools will work, although their usage might be a little c ...
- python基础语法3 元组,字典,集合
元组: ========================元组基本方法===========================用途:存储多个不同类型的值定义方式:用过小括号存储数据,数据与数据之间通过逗号 ...
- 为什么在 Java 中128==128返回false,而127==127返回true呢?
为什么在 Java 中128==128返回false,而127==127返回true呢? 有这样一段代码 Integer a=127; Integer b=127; System.out.printl ...
- jquery.lazyload.js-v1.9.1延时加载插件,已兼容ie6和各大浏览器
来源:http://www.jq22.com/jquery-info390 使用前要求: img的设置: 1.class要配上“lazy”: 2.用data-original代替src: 3.如果想要 ...
- hadoop spark合并小文件
一.输入文件类型设置为 CombineTextInputFormat hadoop job.setInputFormatClass(CombineTextInputFormat.class) sp ...
- 【数论】[素数筛,phi]P3601签到题
题目描述 给出l,r,要求求出\(\sum_{i = l}^r (i - phi[i]) mod 666623333\) \(1\leq l\leq r\leq 10^{12}\),\(r - l \ ...
- FDQuery Out of memory
4万行记录 FDQuery查询 Out of memory sql server 可以查询成功 First chance exception at $7505D722. Exception class ...
- WAMP完整配置教程(启用php extensions、修改端口、允许外网访问、wamp绑定域名)。
作为一名php爱好者,很希望自己的写的代码能够快速的在浏览器页面展现出来,wamp是一款集成很完善.很方便的软件,我刚开始研究的时候,会因为对于wamp的不熟悉,导致修改一点点配置就会在百度查好久,这 ...
- SpringBoot(十六):SpringBoot2.1.1集成fastjson,并使用fastjson替代默认的MappingJackson
springboot2.1.1默认采用的json converter是MappingJackson,通过调试springboot项目中代码可以确定这点.在springboot项目中定义WebMvcCo ...