当前位置: 首页 > article >正文

Salting technique

Join salt technique

Salting is a technique used to distribute data more evenly across partitions, preventing skewness and improving the efficiency of certain operations such as joins in distributed computing frameworks like Apache Spark.

example
  • 参考链接

https://medium.com/@nikaljeajay36/understanding-salting-in-spark-a-practical-guide-bf30f4525f64

import org.apache.spark.sql.functions.{expr, col, lit, concat, floor, rand}

// Generate data for cities
val cityData = Seq.fill(1000)("Aurangabad") ++ Seq.fill(100)("Mumbai") ++ Seq.fill(10)("Chennai") ++ Seq("Hyderabad")
// Create DataFrame for cities
val cityDF = cityData.toDF("city")

这里 Aurangabad 是一个倾斜 key, skew key

val saltedCityDF = cityDF.
   withColumn("Left_city_salt_key", concat(col("city"), lit("_"), floor(rand(123456) * 19)))
// Generate data for states
val stateData = Seq(("Aurangabad", "Maharashtra"), 
                    ("Mumbai", "Maharashtra"), 
                    ("Chennai", "TamilNadu"),
                    ("Hyderabad", "Telangana"))

// Create DataFrame for states
val stateDF = stateData.toDF("cityname", "state")

对维度表,使用 explode 加 salt

val saltedStateDF = stateDF.
withColumn("salt_key", expr("explode(array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19))")).
withColumn("right_city_salt_key", concat(col("cityname"), lit("_"), col("salt_key")))


// Perform inner join between salted city names and salted city names in states
val joinedDF = saltedCityDF.join(saltedStateDF, saltedCityDF("Left_city_salt_key") === saltedStateDF("right_city_salt_key"), "inner")

scala> joinedDF.count
res32: Long = 1111

进行校验

scala> val originDF = cityDF.join(stateDF, cityDF("city") === stateDF("cityname"), "inner")
originDF: org.apache.spark.sql.DataFrame = [city: string, cityname: string ... 1 more field]

scala> originDF.count
res33: Long = 1111

Salting is a powerful technique for improving the performance of distributed computing operations such as joins in Apache Spark. By adding randomness to the partition key, salting helps to distribute data more evenly across partitions, reducing data skew and improving resource utilization. By implementing salting in your Spark applications, you can achieve better performance and scalability, especially when dealing with skewed datasets.

Salt 基本原则

a random number is appended to keys in big table with skew data from a range of random data and the rows in small table with no skew data are duplicated with the same range of random numbers.

Agg salt technique

import org.apache.spark.sql.functions._

df.withColumn("salt", (rand * n).cast(IntegerType))
  .groupBy("salt", groupByFields)
  .agg(aggFields)
  .groupBy(groupByFields)
  .agg(aggFields)

http://www.kler.cn/news/302574.html

相关文章:

  • flink中startNewChain() 的详解
  • Qt-QWidget的font属性(18)
  • 2.ChatGPT的发展历程:从GPT-1到GPT-4(2/10)
  • Linux 管道
  • vue原理分析(十一)研究new Vue()中的initRender
  • 基于深度学习的结构优化与生成
  • 深入理解Kotlin中的异步网络请求处理
  • JavaScript 将 json 美化输出
  • 前端速通面经八股系列(八)—— React篇(上)
  • 基于鸿蒙API10的RTSP播放器(八:音量和亮度调节功能的整合)
  • 数据结构之折半插入排序概念、折半插入排序的具体步骤、折半插入排序的具体代码示例
  • 摊牌了!一文教会你轻松上手豆包MarsCode 编程助手!
  • Android的内核
  • 【STM32】外部中断
  • 数据结构 - 栈
  • 多态(c++)
  • 怎样还原空白试卷?2024教你快速还原空白试卷的软件
  • Python 最小公倍数计算器:从基础到应用
  • 鸿蒙-沉浸式pc端失效
  • 深入理解全连接层:从线性代数到 PyTorch 中的 nn.Linear 和 nn.Parameter
  • Unity Shader实现简单的各向异性渲染(采用各向异性形式的GGX分布)
  • 优化销售流程:免费体验企元数智小程序合规分销系统!
  • Idea 2021.3 破解 window
  • vue3常见的bug 修复bug
  • 力扣每日一题:1372.二叉树中的最长交错路径
  • 腾讯云2024年数字生态大会开发者嘉年华(数据库动手实验)TDSQL-C初体验
  • 62. 不同路径
  • 户用光伏业务市场开发的步骤
  • 走进低代码报表开发(二):高效报表设计新利器
  • 基于SpringMVC的API灰度方案