Spark SQL 抽样函数 ——TABLESAMPLE 的坑点
最近需要实现一段 Spark SQL 逻辑,对数据集进行抽样指定的行数。
由于数据集较大,刚开始的逻辑是,取窗口函数随机排序后 row_number 的前 n 行。但运行速度较慢,所以想起了 TABLESAMLE 函数,支持直接取 Rows, 尝试后发现速度特别快,基本上几秒内就完成对亿级数据的采样。所以好奇就去查看文档和代码逻辑。
The
TABLESAMPLEstatement is used to sample the table. It supports the following sampling methods:
TABLESAMPLE(xROWS): Sample the table down to the given number of rows.TABLESAMPLE(xPERCENT): Sample the table down to the given percentage. Note that percentages are defined as a number between 0 and 100.TABLESAMPLE(BUCKETxOUT OFy): Sample the table down to axout ofyfraction.Note:
TABLESAMPLEreturns the approximate number of rows or fraction requested.
文档中没有对实现逻辑有过多的说明,所以去代码中找问题。
源码中,匹配 SampleByRowsContext 时,调用的方法是 Limit(expression(ctx.expression), query),也就是说和 limit rows 是一个逻辑。
而 SampleByPercentileContext 实现的才是随机采样。
所以,如果对抽样的随机性有要求,还是老老实实用 SampleByPercentileContext,或者窗口函数。
附 相关代码:
/**
* Add a [[Sample]] to a logical plan.
*
* This currently supports the following sampling methods:
* - TABLESAMPLE(x ROWS): Sample the table down to the given number of rows.
* - TABLESAMPLE(x PERCENT) [REPEATABLE (y)]: Sample the table down to the given percentage with
* seed 'y'. Note that percentages are defined as a number between 0 and 100.
* - TABLESAMPLE(BUCKET x OUT OF y) [REPEATABLE (z)]: Sample the table down to a 'x' divided by
* 'y' fraction with seed 'z'.
*/
private def withSample(ctx: SampleContext, query: LogicalPlan): LogicalPlan = withOrigin(ctx) {
// Create a sampled plan if we need one.
def sample(fraction: Double, seed: Long): Sample = {
// The range of fraction accepted by Sample is [0, 1]. Because Hive's block sampling
// function takes X PERCENT as the input and the range of X is [0, 100], we need to
// adjust the fraction.
val eps = RandomSampler.roundingEpsilon
validate(fraction >= 0.0 - eps && fraction <= 1.0 + eps,
s"Sampling fraction ($fraction) must be on interval [0, 1]",
ctx)
Sample(0.0, fraction, withReplacement = false, seed, query)
}
if (ctx.sampleMethod() == null) {
throw QueryParsingErrors.emptyInputForTableSampleError(ctx)
}
val seed = if (ctx.seed != null) {
ctx.seed.getText.toLong
} else {
(math.random() * 1000).toLong
}
ctx.sampleMethod() match {
case ctx: SampleByRowsContext =>
Limit(expression(ctx.expression), query)
case ctx: SampleByPercentileContext =>
val fraction = ctx.percentage.getText.toDouble
val sign = if (ctx.negativeSign == null) 1 else -1
sample(sign * fraction / 100.0d, seed)
case ctx: SampleByBytesContext =>
val bytesStr = ctx.bytes.getText
if (bytesStr.matches("[0-9]+[bBkKmMgG]")) {
throw QueryParsingErrors.tableSampleByBytesUnsupportedError("byteLengthLiteral", ctx)
} else {
throw QueryParsingErrors.invalidByteLengthLiteralError(bytesStr, ctx)
}
case ctx: SampleByBucketContext if ctx.ON() != null =>
if (ctx.identifier != null) {
throw QueryParsingErrors.tableSampleByBytesUnsupportedError(
"BUCKET x OUT OF y ON colname", ctx)
} else {
throw QueryParsingErrors.tableSampleByBytesUnsupportedError(
"BUCKET x OUT OF y ON function", ctx)
}
case ctx: SampleByBucketContext =>
sample(ctx.numerator.getText.toDouble / ctx.denominator.getText.toDouble, seed)
}
}
Spark SQL 抽样函数 ——TABLESAMPLE 的坑点的更多相关文章
- Spark SQL 自定义函数类型
Spark SQL 自定义函数类型 一.spark读取数据 二.自定义函数结构 三.附上长长的各种pom 一.spark读取数据 前段时间一直在研究GeoMesa下的Spark JTS,Spark J ...
- Spark SQL 用户自定义函数UDF、用户自定义聚合函数UDAF 教程(Java踩坑教学版)
在Spark中,也支持Hive中的自定义函数.自定义函数大致可以分为三种: UDF(User-Defined-Function),即最基本的自定义函数,类似to_char,to_date等 UDAF( ...
- 详解Spark sql用户自定义函数:UDF与UDAF
UDAF = USER DEFINED AGGREGATION FUNCTION Spark sql提供了丰富的内置函数供猿友们使用,辣为何还要用户自定义函数呢?实际的业务场景可能很复杂,内置函数ho ...
- Spark学习之路(十一)—— Spark SQL 聚合函数 Aggregations
一.简单聚合 1.1 数据准备 // 需要导入spark sql内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSess ...
- Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations
一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...
- 小白学习Spark系列四:RDD踩坑总结(scala+spark2.1 sql常用方法)
初次尝试用 Spark+scala 完成项目的重构,由于两者之前都没接触过,所以边学边用的过程大多艰难.首先面临的是如何快速上手,然后是代码调优.性能调优.本章主要记录自己在项目中遇到的问题以及解决方 ...
- Spark注册UDF函数,用于DataFrame DSL or SQL
import org.apache.spark.sql.SparkSession import org.apache.spark.sql.functions._ object Test2 { def ...
- Spark SQL 函数全集
org.apache.spark.sql.functions是一个Object,提供了约两百多个函数. 大部分函数与Hive的差不多. 除UDF函数,均可在spark-sql中直接使用. 经过impo ...
- Spark SQL内置函数
Spark SQL内置函数官网API:http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.fun ...
- Spark Sql的UDF和UDAF函数
Spark Sql提供了丰富的内置函数供猿友们使用,辣为何还要用户自定义函数呢?实际的业务场景可能很复杂,内置函数hold不住,所以spark sql提供了可扩展的内置函数接口:哥们,你的业务太变态了 ...
随机推荐
- private priv 私人 pri=prim first v=self 自己第一
private priv 私人 pri=prim first v=self 自己第一 private v自己-私人的 pri 来自PIE*per,向前,穿过 pri = pre 向前(这么理解也说的过 ...
- linux 系统目录详解
tmpfs 的优势: 1,动态文件系统的大小. 2,tmpfs 的另一个主要的好处是它闪电般的速度.因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的. 3,tmpfs 数 ...
- MySQl出现ERROR 1045 (28000): Access denied for user 'root'@'localhost'解决方法
描述 使用到是阿里云服务器,系统为cent Os,给某个账户授权之后,root的账户就登录不进去了,原本root账户设置好了远程连接的权限了,网上搜索了一大堆,终于自己摸索得到了几个方法 产生原因 r ...
- 遇到百张数据表也不怕,Java自动生成实体、Controller、DAO、Service以及Service实现类
一.说明 该工具类实现以下功能: 1.简单的controller方法 2.自动生成Dao类 2.自动生成Service类 2.自动生成ServiceImpl类 二.连接数据库 // 数据库配置信息 p ...
- linux磁盘管理、网络
一 磁盘管理 1 查看磁盘空间的占用 df -h 显示人类易读的方式 linux下磁盘命名格式 /dev/sd[a-z] 2 查看目录的占用空间 du -s 查看目录 -h 显示人类易读的方式 du ...
- Vite+TS项目:论如何便捷的使用pinia
这里给大家分享我在网上学习总结出来的一些知识,希望对大家有所帮助 pinia 介绍 vue新一代状态管理库,相当于vuex 特性 1.像定义components一样定义store 2.支持ts 3.去 ...
- 利用kali自带的msfvenom工具生成远程控制软件
一.首先还是得打开postgresql service postgresql start 然后让我们看看它有哪些功能 部分参数 -p 选择一个载荷,或者说一个模块吧. -i 载荷列表 -f 生成的文件 ...
- FineReport 自定义工具栏样式
虽然FR界面的工具栏已经很商业化,很好看了,但是总会有那么些需求希望你可以修改工具栏的样式. 修改工具栏样式的主要思路是: 通过JQ选择器选中需要调整的元素,然后修改他们的样式 接下来,我们尝试着对工 ...
- 绚烂之境:Python Rich,让终端输出更炫酷!
转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/c8c7bd95.html 初识rich 你好,我是测试蔡坨坨. 在代码的世界里,每一行都是一个故事,每一个变量都是一个角色, ...
- Scala 类和对象与Java的对比
一.包 1 package com{ 2 3 import com.atguigu.scala.Inner 4 5 // 在外层包中定义单例对象 6 object Outer{ 7 var out: ...